Realtime protocol
wss://stream.kolwire.com/v1/stream?token=<stream token> delivers events with sub-second latency. The SDK StreamClient (Node, browsers) and kolwire.realtime.StreamClient (Python) implement everything below — reconnects, resume and de-duplication included.
Messages
All frames are JSON text.
Client → server
json
{ "type": "subscribe", "id": "sub_1", "events": ["call.transcript.*", "call.ended"], "call_id": "call_123", "last_event_id": "evt_…" }
{ "type": "unsubscribe", "id": "sub_1" }
{ "type": "ping" }events— exact types or wildcard patterns (call.*,*). Omit for everything the token allows.call_id— only events of one call.last_event_id— resume: events after this id are replayed (within the retention window).
Server → client
- An event envelope
{ "id", "type", "workspace_id", "created_at", "data" }, or the same wrapped as{ "type": "event", "event": { … } }. { "type": "subscribed", "id" }acknowledging a subscription.{ "type": "error", "code", "message" }.{ "type": "ping" }→ answer{ "type": "pong" }(and vice versa).
Reconnect & resume
On disconnect, reconnect with exponential backoff (0.5 s → 30 s with jitter), fetch a fresh token if the old one may have expired, and resubscribe with last_event_id set to the last event you processed. De-duplicate by event id, since resume may replay a few events.
Close codes
| Code | Meaning | What to do |
|---|---|---|
1000 | Normal close | — |
4001 | Token missing, invalid or expired | Mint a new token and reconnect. |
4003 | Token valid but not allowed (scope / call) | Don't retry with the same token. |
1006 / 1011 | Network drop / server error | Reconnect with backoff. |
SDK usage
ts
import { StreamClient } from "@kolwire/sdk/realtime";
const stream = new StreamClient({
url: "wss://stream.kolwire.com/v1/stream",
token: () => fetch("/stream-token").then((r) => r.json()).then((j) => j.token),
subscribe: { events: ["call.*"] },
});
stream.on("call.ended", (e) => console.log(e.data.call_id, e.data.status));
stream.on("close", ({ code }) => console.log("closed", code));
await stream.connect();
const subId = stream.subscribe({ call_id: "call_123", events: ["call.transcript.final"] });
stream.unsubscribe(subId);
stream.close();python
from kolwire.realtime import StreamClient
stream = StreamClient(url="wss://stream.kolwire.com/v1/stream", token=fetch_token)
stream.on("call.ended", lambda e: print(e.data.call_id, e.data.status))
await stream.connect()
async for e in stream.events("call.transcript.*"):
print(e.data.text)On Node < 22 (no global WebSocket) install ws and pass WebSocket from it: new StreamClient({ ..., WebSocket }).