Skip to content

Repository files navigation

put.io boncuk

putio-sockjs

SockJS client for real-time put.io events.

CI npm version license

Installation

npm install @putdotio/socket-client

Quick Start

import { createPutioSocketClient, EVENT_TYPE } from "@putdotio/socket-client";

const client = createPutioSocketClient({
  token: process.env.PUTIO_TOKEN!,
});

const unsubscribe = client.on(EVENT_TYPE.TRANSFER_UPDATE, (payload) => {
  console.log(payload);
});

client.on(EVENT_TYPE.CONNECT, () => {
  client.send({
    type: "custom",
    value: { hello: "world" },
  });
});

// Later, during teardown:
unsubscribe();
client.close();

Connection Lifecycle

Call client.close() during teardown. Closing is idempotent and permanently stops heartbeat checks and pending reconnects for that client. Create a new client to connect again.

Transient closures trigger up to 10 reconnect attempts: the first is immediate, then delays start at 100 ms and double after each failed attempt. A successful connection resets the retry budget and emits connect followed by reconnect. Normal closure (1000), server error (1011), and application codes from 4000 up, such as unauthorized (4001), stop the client without retrying.

Events

EVENT_TYPE names every socket event, and SocketEvents maps each one to its payload type:

import { EVENT_TYPE, type SocketEvents } from "@putdotio/socket-client";

const onTransferUpdate = (transfer: SocketEvents["TransferUpdate"]["value"]) => {
  console.log(transfer.id, transfer.status);
};

client.on(EVENT_TYPE.TRANSFER_UPDATE, onTransferUpdate);

Docs

License

This project is available under the MIT License.

Releases

Used by

Contributors

Languages