Swiftybase
Function StackSSE

Upgrade to SSE

Initiates a protocol upgrade to establish a Server-Sent Events (SSE) connection with the client. This enables the server to push real-time updates to the client over a single long-lived HTTP connection.

Config

  • Allowed Origins
    A list of origins allowed to establish SSE connections. Use this to enforce CORS policies and restrict access to trusted clients.

  • Ping Duration
    Interval at which heartbeat/ping messages are sent to the client to keep the connection alive. This helps prevent proxies and load balancers from closing idle connections.

  • Inactivity Duration
    The maximum allowed time of inactivity (no messages sent) before the server closes the SSE connection. Helps to clean up unused or stale connections.

Output

  • Returns:
    A SSEController instance, which can be used to send events to the client, close the connection, or monitor its status.

Example: Upgrade to SSE

In this example, we upgrade the connection to Server-Sent Events (SSE) and use the Send SSE function to send a welcome message. Inside a loop, we send an incrementing index as data every 2 seconds.

Use the returned result from the Send SSE function to check if the connection is still active. Alternatively, use the Is SSE Active function to verify the connection status. Combine this with the Precondition function to cleanly exit the loop if the connection closes unexpectedly.
Once the loop ends, remember to close the SSE connection with the appropriate function.

Upgrade to SSE

Upgrade To SSE

Fill in the options such as Allowed Origins, Ping Duration, and Inactivity Duration.

Send Initial Event

Send SSE

Provide the SSEController returned from the upgrade function to the Send SSE function.
Fill out the event name and data fields to send a message to the client.

Loop and Send Index as Message

Use a loop to send the current index value every 2 seconds as a message.
You can use Precondition or the controller's state to break the loop if the connection is no longer active.

Loop SSE Message

On this page