Node.js SDK — Realtime
From the server side, use sdk.socket.emit() to broadcast events to WebSocket subscribers.
Emit an event
Section titled “Emit an event”// Emit to all subscribers on a channelsdk.socket.emit(event, data, roomId)| Parameter | Type | Description |
|---|---|---|
event | string | Event name — clients listen for this |
data | any | Payload to send |
roomId | string | Channel topic — must match what clients subscribed to |
Examples
Section titled “Examples”// After a DB update, notify subscribersawait sdk.db.query('UPDATE orders SET status = ? WHERE id = ?', ['shipped', orderId])sdk.socket.emit('order:updated', { id: orderId, status: 'shipped' }, `orders/${orderId}`)
// Broadcast to all in a chat roomsdk.socket.emit('message', { userId, text, timestamp: Date.now() }, `chat/general`)
// Stream AI tokensfor await (const token of stream) { sdk.socket.emit('ai:token', { token }, `ai-chat/${sessionId}`)}sdk.socket.emit('ai:done', {}, `ai-chat/${sessionId}`)DB change events (automatic)
Section titled “DB change events (automatic)”For DB table subscriptions (channel('tableName') with no slash on the client), you don’t need sdk.socket.emit(). ChangeTracker handles it:
// Just run the SQL — ChangeTracker broadcasts UPDATE to all channel('orders') subscribersawait sdk.db.query('UPDATE orders SET status = ? WHERE id = ?', ['shipped', orderId])See Database Subscriptions for details.