# Node.js SDK — Realtime

> Broadcast WebSocket events from your Cloudflare Worker. Emit named events to any channel subscriber with sdk.socket.emit().

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

## Emit an event

```ts
// Emit to all subscribers on a channel
sdk.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

```ts
// 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:

```ts
// 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](/features/realtime/subscriptions) for details.
