Skip to content

WebSocket Example

Demonstrates real-time WebSocket support alongside REST APIs.

Source: examples/websocket/

  • Two-phase authentication — JWT sent over encrypted WebSocket, not query params
  • Connection lifecycle$connect, $disconnect, $default handlers
  • Connection storeconnectionStore from @venturekit/runtime
  • Send to user — deliver messages to a specific user across sessions
  • Broadcast — send to all authenticated connections
  • Tenant-scoped messagingsendToTenant() for multi-tenancy
  • Idle timeout — configurable connection timeout
  • REST + WebSocket — both APIs in the same project
websocket: {
enabled: true,
routeSelectionExpression: '$request.body.action',
idleTimeoutSec: 600,
throttleRateLimit: 100,
}
{ "action": "auth", "token": "<jwt>" }
{ "action": "ping" }
{ "action": "broadcast", "data": { "text": "Hello!" } }
{ "action": "send", "to": "<userId>", "data": { "text": "Hi!" } }
Terminal window
cd examples/websocket
npm install
vk dev
# Connect with wscat
wscat -c "wss://<ws-api-id>.execute-api.eu-west-1.amazonaws.com/dev"
> {"action": "auth", "token": "<jwt>"}