Skip to main content

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-in EventSource API handles reconnection automatically.
If you pass the token via the 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 a types query parameter with a comma-separated list to receive only the events you care about.
You can also restrict the types at the module level so no client can subscribe to events outside the allowed set.

Replay and cursor

Events are persisted in the kavach_stream_events table. If a client disconnects and reconnects, pass since to receive everything it missed.
The browser 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 default requireAuth: true. Every connection must present a valid Bearer token, either in the Authorization header or as the token query parameter.
When the token is invalid, the stream sends a single error event and closes.
To disable auth for local development or internal-only deployments:
Never disable auth in production. The stream exposes audit events and agent lifecycle data.

Configuration

Connection limits

The stream rejects connections beyond maxConnections 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.

Emitting events from plugins

Any part of your application can emit to the stream.
Integrate with the webhooks module to fire both a webhook and a stream event from the same action.

Module API

Last modified on April 29, 2026