npm install @putdotio/socket-clientimport { 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();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 emits connect followed by reconnect; it resets the retry budget
only once it has stayed open for 10 seconds, so a server that accepts and then
drops connections still backs off and exhausts the attempts.
Normal closure (1000), server error (1011), and application codes from 4000 up,
such as unauthorized (4001), stop the client without retrying.
EVENT_TYPE names every socket event, and SocketEvents types the payload of
each server event:
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);This project is available under the MIT License.
