What event streaming is
Event streaming gives you a persistent, real-time connection to KavachOS events via Server-Sent Events (SSE). The moment an agent is revoked, a budget is exceeded, or an anomaly is detected, connected clients receive the event, no polling required. Events are also persisted to the database so you can replay anything you missed.Streaming vs webhooks
Both webhooks and SSE carry the same events, but they serve different use cases.
Use webhooks when you need durable delivery to an external service. Use event streaming when you need a live view, a security dashboard, an admin feed, or a CI script watching for
anomaly.detected.
Setup
handleRequest returns null for any request that is not a valid SSE request (wrong path, wrong Accept header, or non-GET method). This makes it safe to call inside a catch-all handler.Connecting from a browser
The browser’s built-inEventSource API handles reconnection automatically.
Authorization header instead, use the eventsource package or a fetch-based polyfill, since the native EventSource does not support custom headers.
Connecting from Node.js
Event types
Filtering events
Pass atypes query parameter with a comma-separated list to receive only the events you care about.
Replay and cursor
Events are persisted in thekavach_stream_events table. If a client disconnects and reconnects, pass since to receive everything it missed.
EventSource can also pass the last received event ID via the Last-Event-ID header, which the browser manages automatically when you set the id: field on SSE events. KavachOS uses the Last-Event-ID value as the replay cursor.
replay returns up to 1000 events in descending order (newest first). Apply your own pagination on top if you need to page through large windows.
Auth requirements
By defaultrequireAuth: true. Every connection must present a valid Bearer token, either in the Authorization header or as the token query parameter.
error event and closes.
Configuration
Connection limits
The stream rejects connections beyondmaxConnections with a 503 Too many connections response. Size this based on your deployment: a single process can comfortably handle hundreds of concurrent SSE connections; above that, consider a pub/sub layer (Redis, NATS) in front of the module.
Heartbeat
The server sends: heartbeat comments on the configured interval (default 30 seconds) to keep load balancers and proxies from closing idle connections. No action is required on the client side, EventSource ignores comment lines.