@deploybaseline/ws
v0.14.0
Published
WebSocket endpoints for Baseline emulators: handshake, connection registry, heartbeat, and resume buffer
Readme
@deploybaseline/ws
The WebSocket kit for Baseline emulators. Slack Socket Mode runs on it. It is opt-in: the CLI does not bundle it, and loads it only when a WebSocket feature is used.
npx -p @deploybaseline/cli -p @deploybaseline/ws baseline start
# or, in a project
npm install -D @deploybaseline/cli @deploybaseline/wsFor built-in providers, on core's app.upgrade:
import { webSocketRoute, messageText } from "@deploybaseline/ws";
const registry = webSocketRoute(app, "/rooms/:room", {
authorize: ({ url, params }) => (url.searchParams.get("token") ? { room: params.room } : 401),
onConnection(connection) {
connection.send({ type: "hello" });
connection.socket.on("message", (data) =>
registry.broadcast({ text: messageText(data) }, (other) => other.meta.room === connection.meta.room),
);
},
});webSocketRoutechecks the handshake, accepts it, tracks the connection, pings it every 30 seconds, and closes it with code 1001 when the service stops.ConnectionRegistry: open connections with message counts, filtered broadcast, change listeners, andcloseAll().startHeartbeat: pings on an interval and drops a client that misses too many pongs.ResumeBuffer: the last events of a session, numbered from 1, for resuming after a reconnect.
WebSocket endpoints run in the CLI (including --single-port) and createEmulator. Custom emulators cannot serve them yet. Tests can connect with connectWs from @deploybaseline/testing.
See WebSockets.
