Skip to main content
WebSocket provides a full-duplex communication channel for low-latency real-time updates in SnackBase.

Overview

The SnackBase SDK automatically uses WebSocket when available, providing real-time updates with minimal latency:

WebSocket vs SSE

The SDK automatically falls back to SSE if WebSocket is not available.

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:
  1. Open DevTools (F12)
  2. Go to Network tab
  3. Filter by WS (WebSocket)
  4. Select the WebSocket connection
  5. 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