@samveys/broadcast
From packages/broadcast/README.md in the framework repository.
Relays messages between app instances, so a WebSocket or SSE client on one server receives what another server publishes. The database driver needs no extra infrastructure: every instance polls one table.
Use it
import { createApp } from '@samveys/core'
import { database, sqlite } from '@samveys/database'
import { http } from '@samveys/http'
import { broadcast, databaseBroadcast } from '@samveys/broadcast'
export const app = createApp({
name: 'chat',
plugins: [
http(),
database({ default: 'main', connections: { main: sqlite({ file: 'storage/app.db' }) } }),
broadcast({ driver: ({ make }) => databaseBroadcast({ database: () => make('db') }) }),
],
})The broadcast binding then offers:
| Method | Does |
|---|---|
publish(channel, event, payload) | Sends to every instance |
subscribe(channel, listener) | Listens on this instance; returns an unsubscribe function |
toSse(channel, { lastEventId, signal }) | An async iterable for sse() that replays after Last-Event-ID, then streams |
join(channel, socket) / leave(channel, socket) | Fan-out to WebSocket connections as { event, payload } JSON |
Drivers
| Driver | Use it for |
|---|---|
databaseBroadcast({ database, pollMs = 250, retentionMs = 600000 }) | Several instances. Table samveys_broadcast; each instance starts at the current highest id and prunes old rows |
memoryBroadcast() | One instance, and tests |
Delivery is at most once per instance and ordered by id. A client that reconnects with Last-Event-ID gets what it missed within the retention window.