Publish SSE
Publishes a Server-Sent Events (SSE) message to all registered subscribers by channel name.
Config
-
Event (optional)
A pre-constructedSSEEvent
instance. If provided, the individual fields below will be ignored. -
Id
Optional unique identifier for the event. Useful for reconnection and deduplication. -
Type
The name/type of the event. This appears in theevent:
field of the SSE payload on the client. -
Data
The message payload (text or JSON). This is the content delivered to the client. -
Retry
Optional reconnection interval. Informs the client how long to wait before retrying if the connection is lost. -
Channel Name
The name of the channel to associate the event with. Clients subscribed to the same channel will receive the event. -
Publisher
An instance ofSSEPublisher
, which should be created once globally to handle broadcasting events to multiple clients.
Example: Publish SSE Event to Channel Subscribers
In this example, we upgrade the connection to SSE and use an SSEPublisher
to broadcast an event to multiple clients subscribed to the same channel.
It also shows how to create a specialized publisher, like a Redis SSE Pub/Sub Publisher, and register it globally to be reused across handlers.
Upgrade to SSE
Use the Upgrade to SSE
function and configure the following:
- Allowed Origins
- Ping Duration
- Inactivity Duration
Create Publisher Variable
Create a variable named publisher
of type SSEPublisher
as a late variable, so you can initialize it later.
Check the Create Variable
function to learn how to declare a late variable that can be initialized when needed.
Check if SSE Publisher is Registered
Use Has Registry Value
to check if a global SSEPublisher
already exists:
- If true: Retrieve it using
Get Registry Value
and assign it topublisher
. Make sure to cast it toSSEPublisher
. - If false: Proceed to create a new Redis-based SSE publisher (see Step 4).
Currently, only Redis-based publishers are supported for cross-server event delivery.
Future support will include Kafka and DB-based pull delivery systems.
Initiate Redis Client
Use Redis Connect
and assign the result to a variable named redisDb
.
Example Redis URL:
rediss://default:[email protected]:6379
Services like Upstash offer free Redis databases to test with.
Create & Register Redis SSE Publisher
Create the publisher using New Redis SSE Pub Sub Publisher
, passing:
- The
redisDb
instance - A sync channel name (can be anything)
Assign the result to the publisher
variable.
Start the publisher with Start SSE Publisher
.
Please create and start the SSEPublisher
once during server startup using a server hook.
Access it later via Get Registry Value
. Avoid initializing resources inside API handlers.
Register SSE Connection
Register the SSE connection to a channel using Register SSE
:
- The controller from
Upgrade to SSE
- A channel name
- The
publisher
instance
Create SSE Event
Use New SSE Event
to create an event and pass the name
query parameter as the event type.
Check New SSE Event
for more details on how to construct an event.
Loop and Send Message
Use a loop to update the event’s data
field with the current index every 2 seconds.
Then call Publish SSE
, passing the event, channel name, and the publisher.
Remove Connection
After the loop ends, clean up by calling Remove SSE
, providing the controller ID and channel name to close the connection.