Skip to main content

SSE (Server-Sent Events)

m4trix uses SSE for streaming agent responses to HTTP clients. When agents emit events to a channel with proxy.sse(), those events are streamed as SSE.

How It Works

  1. Client sends a POST with a JSON payload.
  2. expose() publishes a start event to the main channel.
  3. Agents run and emit events to the client channel.
  4. Events are streamed as SSE to the response.

Response Format

Consuming in the Browser

Or use fetch with ReadableStream for POST:

WebSocket

m4trix focuses on SSE (request → stream response). For bidirectional WebSocket, you would need to adapt the event plane or use a separate WebSocket layer. The useSocketConversation hook in @m4trix/react can work with WebSocket backends when your server exposes a compatible protocol.

Backpressure

The SSE proxy respects backpressure: if the client is slow to consume, the underlying stream will backpressure. For LLM streams, emitting chunk-by-chunk naturally paces the flow.

Chunking

When streaming LLM output, emit each token or logical chunk as a separate event:
For lower-level stream processing (rechunking, batching), use the Pump from @m4trix/stream — see Streaming, Proxies & Adapters.