Skip to main content
The Realtime service provides WebSocket and Server-Sent Events (SSE) connections for live data updates in your SnackBase collections.

Overview

Subscribe to collection changes and receive instant updates when records are created, updated, or deleted:

Connection Methods

SnackBase realtime supports two connection methods: The SDK automatically uses WebSocket when available and falls back to SSE.

Getting Started

1. Connect

Connect to the realtime service:
The SDK requires an authenticated user for realtime connections. Ensure the user is logged in before connecting.

2. Subscribe to Collections

Subscribe to events on a collection:

3. Listen for Events

Listen for specific events:

4. Disconnect

Disconnect when done:

Connection States

Monitor the connection state:

Subscription Events

Subscribe to specific operations:

Event Data Structure

Events include the complete record data:

Multiple Subscriptions

Subscribe to multiple collections:

Wildcard Events

Use wildcards to listen to all events:

Unsubscribing

Unsubscribe from a collection:
Remove event listeners:

Complete Example

React Integration

Use realtime with React:

Reconnection

The SDK automatically reconnects on connection loss:
Reconnection uses exponential backoff:

Authentication and Realtime

Token refresh is handled automatically:

Error Handling

Handle realtime errors:

Best Practices

1. Subscribe to What You Need

Only subscribe to collections and operations you need:

2. Clean Up Subscriptions

Always unsubscribe when done:

3. Handle Connection States

Show connection status to users:

Next Steps

  • WebSocket - WebSocket-specific features
  • SSE - Server-Sent Events
  • Events - Event reference