Skip to main content
Sonamu makes it easy to create SSE endpoints using the @stream decorator. Use the SSE connection object created by ctx.createSSE() to send events to clients.

@stream Decorator

A decorator for defining SSE endpoints.

Basic Usage

@stream Options

Stream type
Supported types:
  • 'sse': Server-Sent Events
  • (WebSocket support planned for future)

SSEConnection API

The SSE connection object created by ctx.createSSE().

closed

A read-only property indicating whether the connection has been closed. Becomes true after the client disconnects or end() is called.

onClose(callback)

Registers a callback to be invoked when the connection closes. Fires on both client-side disconnection and end() calls. Useful for cleaning up timers or external resources.

publish(event, data)

Sends an event to the client.
Type safety:

end()

Closes the connection.
Auto-sends: event: end, data: END

Practical Examples

1. Real-time Notifications

2. Task Progress

3. Live Feed

4. Multiple Events

5. Conditional Sending

Connection Management

Detecting Client Disconnection

Keep-Alive

SSE automatically sends keep-alive signals, but custom implementation is also possible:

Error Handling

Important Notes

Important considerations when writing SSE endpoints:
  1. @stream and @api cannot be used together: Use only the @stream decorator for SSE endpoints
  2. Always call end(): Connection closure is required
  3. Beware of infinite loops: Appropriate wait time needed
  4. Event schema matching: events and publish event names must match
  5. Prevent memory leaks: Clean up timers/listeners

Next Steps

SSE Setup

Configure the SSE plugin

Client Integration

Using SSE in the frontend