Skip to main content
Server-Sent Events (SSE) provide a simple, one-way communication channel for real-time updates from the server to the client.

Overview

The SnackBase SDK automatically falls back to SSE when WebSocket is not available:

SSE vs WebSocket

The SDK prefers WebSocket but falls back to SSE automatically.

SSE URL

The SDK constructs the SSE URL from your base URL:

Subscriptions with SSE

SSE subscriptions are specified at connection time:

Connection Lifecycle

1. Connection

2. State Changes

3. Disconnection

SSE Message Format

Messages are received as text/event-stream:

Automatic Reconnection

SSE has built-in reconnection:

Authentication

SSE connections include the authentication token in the URL:
Tokens in URLs may be logged in server access logs. This is a limitation of the SSE protocol.

SSE Limitations

1. One-Way Communication

SSE is server-to-client only:

2. Limited Subscriptions After Connection

With SSE, subscriptions are set at connection time:

3. No Heartbeat

SSE uses connection keep-alive instead of heartbeat:

When to Use SSE

Use SSE when:
  1. WebSocket is unavailable - Older browsers or restrictive networks
  2. Simple updates needed - One-way server updates are sufficient
  3. Lower server load - SSE uses fewer resources than WebSocket

React Example

Cross-Browser Considerations

SSE is supported in most browsers: For IE 11, use an SSE polyfill:

Debugging SSE

Enable logging to debug SSE issues:
Check SSE connection in browser DevTools:
  1. Open DevTools (F12)
  2. Go to Network tab
  3. Filter by EventStream
  4. Select the SSE connection
  5. View messages in Response tab

Common Issues

1. Connection Closes Immediately

Problem: SSE connection closes after opening Solutions:
  • Check authentication token is valid
  • Verify server allows SSE connections
  • Check for proxy/load balancer issues

2. No Events Received

Problem: Connection stays open but no events received Solutions:
  • Verify subscriptions are set before connection
  • Check collection names are correct
  • Ensure server has events to send

3. Frequent Reconnections

Problem: SSE keeps reconnecting Solutions:
  • Check network stability
  • Verify server timeout settings
  • Increase reconnection delay

Performance Tips

1. Minimize Subscriptions

Only subscribe to what you need:

2. Debounce Updates

Debounce rapid updates:

3. Use Connection Pooling

For multiple tabs, use BroadcastChannel:

Next Steps