|
| 1 | +# Server-Sent Events (SSE) |
| 2 | + |
| 3 | +Server-Sent Events (SSE) is a web standard that allows a server to push real-time updates to a client over a single HTTP connection. Unlike WebSockets, SSE is **unidirectional (server → client)** and includes **automatic reconnection**. |
| 4 | + |
| 5 | +For a complete working example demonstrating SSE functionality including connection management, event handling, error handling, and UI integration, see the [SSE Kitchen Sink example](https://studio.ensembleui.com/app/e24402cb-75e2-404c-866c-29e6c3dd7992/screen/rAmWKmUWonS4rWiHuhte) in Ensemble Studio. |
| 6 | + |
| 7 | + |
| 8 | +## Key Features |
| 9 | + |
| 10 | +* **Unidirectional**: Server pushes data to the client |
| 11 | +* **Auto-reconnect**: Automatically reconnects on connection loss |
| 12 | +* **HTTP-based**: Uses standard HTTP (`text/event-stream`) |
| 13 | +* **Event-driven**: Supports event types, IDs, and payloads |
| 14 | +* **Simple API**: Easy to configure and manage |
| 15 | + |
| 16 | +## Common Use Cases |
| 17 | + |
| 18 | +* Real-time notifications and alerts |
| 19 | +* Live data feeds (stocks, sports scores) |
| 20 | +* Progress updates for long-running tasks |
| 21 | +* One-way chat or messaging |
| 22 | +* Live dashboards and monitoring |
| 23 | +* News feeds and social updates |
| 24 | + |
| 25 | +## Define an SSE API |
| 26 | + |
| 27 | +To use SSE in Ensemble, define an API with `type: sse` and provide the SSE endpoint along with optional connection settings. |
| 28 | + |
| 29 | +```yaml |
| 30 | +API: |
| 31 | + sseEvents: |
| 32 | + type: sse # Required |
| 33 | + url: https://sse.dev/test # SSE endpoint |
| 34 | + sseOptions: |
| 35 | + autoReconnect: true # Automatically reconnect |
| 36 | + reconnectDelay: 1000 # Delay (ms) before reconnect |
| 37 | + maxReconnectAttempts: 5 # Max retry attempts |
| 38 | + |
| 39 | + onResponse: # Fired for each SSE event |
| 40 | + executeCode: |
| 41 | + body: | |
| 42 | + // Process event data |
| 43 | + var eventData = response.body.data; |
| 44 | +
|
| 45 | + onError: # Fired on connection error |
| 46 | + executeCode: |
| 47 | + body: | |
| 48 | + console.error('SSE error:', response); |
| 49 | +``` |
| 50 | +
|
| 51 | +## Configuration Options |
| 52 | +
|
| 53 | +| Option | Description | |
| 54 | +| --------------------------------- | ---------------------------------------------- | |
| 55 | +| `type: sse` | **Required** – Enables SSE | |
| 56 | +| `url` | SSE endpoint (`text/event-stream`) | |
| 57 | +| `sseOptions.autoReconnect` | Auto-reconnect on disconnect (default: `true`) | |
| 58 | +| `sseOptions.reconnectDelay` | Delay between retries in ms (default: `1000`) | |
| 59 | +| `sseOptions.maxReconnectAttempts` | Max reconnect attempts (default: `5`) | |
| 60 | +| `onResponse` | Executed for each received event | |
| 61 | +| `onError` | Executed on SSE errors | |
| 62 | + |
| 63 | +## Event Response Structure |
| 64 | + |
| 65 | +Each event received in `onResponse` contains: |
| 66 | + |
| 67 | +```js |
| 68 | +response.body.event // Event name (default: "message") |
| 69 | +response.body.id // Event ID (if provided) |
| 70 | +response.body.data // Event payload (text or JSON) |
| 71 | +``` |
| 72 | + |
| 73 | +## Disconnect from SSE |
| 74 | + |
| 75 | +Use the built-in action to disconnect from an SSE connection: |
| 76 | + |
| 77 | +```yaml |
| 78 | +disconnectSSE: |
| 79 | + apiName: sseEvents |
| 80 | +``` |
| 81 | + |
| 82 | +```javascript |
| 83 | +ensemble.disconnectSSE({apiName: "sseEvents"}); |
| 84 | +``` |
| 85 | + |
| 86 | +## Summary |
| 87 | + |
| 88 | +Ensemble's SSE support provides: |
| 89 | + |
| 90 | +* Simple real-time data streaming |
| 91 | +* Automatic reconnection |
| 92 | +* Built-in lifecycle management |
| 93 | +* Easy UI integration |
| 94 | +* Clean event handling |
| 95 | + |
| 96 | +This makes SSE ideal for **live dashboards, notifications, and streaming updates** without the complexity of WebSockets. |
0 commit comments