Swiftybase
Function StackSSE

Publish SSE

Publishes a Server-Sent Events (SSE) message to all registered subscribers by channel name.

Config

  • Event (optional)
    A pre-constructed SSEEvent 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 the event: 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 of SSEPublisher, 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

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 to publisher. Make sure to cast it to SSEPublisher.
  • 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.

Check SSE Publisher

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 Redis Client

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.

Create SSE Publisher

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

Register SSE

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.

Publish SSE

Remove Connection

After the loop ends, clean up by calling Remove SSE, providing the controller ID and channel name to close the connection.

On this page