The official TypeScript client library for Conduit.
The client handles connection management, message deduplication, and sequencing internally. Your onMessage callback receives deduplicated, ordered payloads.
npm install conduit-clientSee the examples directory for complete working examples.
| Method | Description |
|---|---|
| connect() | Connect to the stream |
| disconnect() | Disconnect from the stream |
| isConnected() | Check if currently connected |
| isConnecting | Check if a connection is in progress |
| Option | Type | Required | Description |
|---|---|---|---|
| orgId | number | Yes | Organization identifier |
| startStreamUrl | string | Yes | URL endpoint for initializing a stream |
| startStreamData | Record<string, unknown> | No | Additional POST body data to send with the start stream request |
| startStreamHeaders | Record<string, string> | No | Custom headers to send with the start stream request |
| onMessage | (message: T) => void | No | Called when a stream message is received |
| onConnect | () => void | No | Called when the stream first connects |
| onReconnect | () => void | No | Called when the stream automatically reconnects after a connection drop |
| onClose | () => void | No | Called when explicitly closed (disconnected) |
| onError | (error: Error) => void | No | Called when a stream or connection error occurs |
The startStreamUrl endpoint must include { conduit: { token, channel_id, url } } in its response.
Returns { isConnected, error } and manages connection lifecycle automatically.
Accepts all configuration options above, plus enabled (boolean, default true) to control whether to connect.
Both ConduitClient and useStream accept a generic type parameter for typed message payloads:
type MyPayload = {
id: string;
value: number;
};
const client = new ConduitClient<MyPayload>({ ... });
// onMessage payload is typed as MyPayloadThe onError callback is called for:
- Network/connection failures
- Stream errors (server-sent
PHASE_ERROR) - Malformed message parsing failures
The client automatically reconnects when the underlying connection drops (e.g., network interruption). onReconnect fires on successful reconnection. When the server shuts down (e.g., during a deploy), it sends a shutdown event and closes the stream. The client then reconnects after the server-provided delay and resumes from the last received event, without calling onError in the meantime. Calling disconnect() stops the stream permanently until connect() is called again.