Node.js SDK — Realtime

From the server side, use sdk.socket.emit() to broadcast events to WebSocket subscribers.

Emit an event

// Emit to all subscribers on a channel
sdk.socket.emit(event, data, roomId)
ParameterTypeDescription
eventstringEvent name — clients listen for this
dataanyPayload to send
roomIdstringChannel topic — must match what clients subscribed to

Examples

// After a DB update, notify subscribers
await 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 room
sdk.socket.emit('message', { userId, text, timestamp: Date.now() }, `chat/general`)
 
// Stream AI tokens
for 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)

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') subscribers
await sdk.db.query('UPDATE orders SET status = ? WHERE id = ?', ['shipped', orderId])

See Database Subscriptions for details.