Overview
The SnackBase SDK automatically uses WebSocket when available, providing real-time updates with minimal latency:WebSocket vs SSE
| Feature | WebSocket | SSE |
|---|---|---|
| Latency | Very low | Low |
| Bidirectional | Yes | No |
| Browser Support | Modern | All |
| Fallback | Yes | Primary |
| Server Load | Higher | Lower |
| Use Case | Interactive | Simple updates |
WebSocket URL
The SDK constructs the WebSocket URL from your base URL:Authentication
WebSocket connections include the authentication token:Connection Lifecycle
1. Connection
2. State Changes
3. Disconnection
Subscriptions
WebSocket subscriptions are sent as messages:Subscription Confirmation
The server confirms subscriptions:Heartbeat
WebSocket connections use heartbeat to detect dead connections:Reconnection
Automatic reconnection with exponential backoff:Manual Reconnection
Force a reconnection:Check Connection Method
The SDK can detect if WebSocket is available:Performance Optimization
1. Batch Operations
Wait for multiple events before updating UI:2. Selective Subscriptions
Only subscribe to operations you need:3. Connection Pooling
For multiple tabs/windows, use a shared worker or broadcast channel:WebSocket Configuration
Configure WebSocket behavior:Debugging WebSocket
Enable logging to debug WebSocket issues:Browser DevTools
Inspect WebSocket messages in browser devtools:- Open DevTools (F12)
- Go to Network tab
- Filter by WS (WebSocket)
- Select the WebSocket connection
- View messages in Messages tab
React Example
Common Issues
1. Connection Fails
Problem: WebSocket connection fails Solutions:- Check that user is authenticated
- Verify base URL is correct
- Check browser console for errors
- Try HTTPS/WSS (some browsers block WS on HTTPS pages)
2. Frequent Disconnections
Problem: Connection drops frequently Solutions:- Increase
maxRealTimeRetries - Check network stability
- Verify server WebSocket timeout settings
3. Events Not Received
Problem: Not receiving realtime events Solutions:- Verify subscription was successful
- Check connection state
- Ensure you’re listening to correct event names
Next Steps
- SSE - Server-Sent Events fallback
- Events - Event reference
- Realtime Overview - Getting started