Skip to content
TypeWire

@tahanabavi/typesocket

v2.2.0

Contract-driven, type-safe Socket.IO client — one Zod contract validated in both directions, with acks, middleware, queueing and a devtools instrumentation seam.

Configuration

ts
const client = new SocketClient(
{
url: "http://localhost:3001",
auth: () => ({ token: getToken() }), // re-invoked on every reconnect
ackTimeoutMs: 10_000,
maxQueueSize: 100,
debug: false,
onValidationError: (error) => reportToSentry(error),
},
wsContracts,
{
onConnect: ({ socketId, attempt }) => console.log(socketId, attempt),
onDisconnect: (reason) => console.log(reason),
onConnectError: (error) => console.error(error.message),
middlewares: [logger],
},
);

client.connect();

Reading config from the environment is prefix-driven rather than hardcoded to Next.js, and only produces keys for variables that are actually set — so it layers cleanly over explicit config:

ts
import { socketConfigFromEnv } from "@tahanabavi/typesocket";

const client = new SocketClient(
{ url: "/", ...socketConfigFromEnv("NEXT_PUBLIC_SOCKET_") },
wsContracts,
);

Recognised suffixes: URL, PATH, AUTO_CONNECT, RECONNECTION, RECONNECTION_ATTEMPTS, RECONNECTION_DELAY, ACK_TIMEOUT, AUTH_TOKEN, QUERY_PARAMS (JSON), TRANSPORTS (comma-separated), DEBUG.