Skip to main content
The @stream decorator creates real-time streaming APIs using Server-Sent Events (SSE).

Basic Usage

Options

type

Specifies the streaming type. Required option
WebSocket is provided via the dedicated @websocket decorator. @stream is for SSE only and the type option accepts "sse" only.

events

Defines event types to send using a Zod schema. Required option

path

Specifies the streaming endpoint path. Default: /{modelName}/{methodName} (camelCase)

guards

Specifies access permissions.

description

Adds a description for the streaming API.

resourceName

Specifies the resource name for the generated service file.

SSE Object Usage

createSSE

Creates an SSE object from Context.

publish

Sends an event to the client.
Type safety:
  • Event names must be defined in the schema
  • Data is automatically validated against the schema

end

Closes the stream connection.
end() sends an “end” event to the client and closes the connection after waiting 200ms.

Complete Example

Using with Other Decorators

@transactional

Write @stream first, then @transactional.

Constraints

1. Cannot be combined with other routing decorators (@api, @websocket, @upload)

@api, @stream, @websocket, and @upload cannot be combined on the same method.
Error message:

2. events is required

3. httpMethod is always GET

@stream automatically sets httpMethod: "GET".

Client Usage (Web)

Sonamu automatically generates SSE client code.

React Example

Logging

The @stream decorator automatically logs:

Example Collection

Next Steps

@api

Create general API endpoints

@transactional

Ensure data consistency with transactions

@cache

Improve performance with result caching

SSE Guide

View detailed SSE guide