Realtime Engine
Reactive Infrastructure
Build interactive, live experiences with our ultra-low latency WebSocket engine. Listen to database changes as they happen, broadcast arbitrary JSON messages between clients, and track global user presence with sub-100ms latency.
Technical Architecture
Sub-100ms Propagation
Built on a distributed messaging cluster, our Realtime engine guarantees sub-100ms message propagation between your clients and infrastructure nodes.
Secure Broadcasting
All realtime channels are protected by JWT-based authentication. Define complex access rules to ensure data only reaches authorized subscribers.
Implementation
01
Initialize a Channel
Create a channel targeting a specific "room" or topic.
JavaScript
typescript
const channel = afribase.channel('room-1');Python
python
channel = client.channel("room-1")Dart / Flutter
dart
final channel = client.channel('room-1');02
Broadcast Messages
Send and listen for custom broadcast messages between clients.
JavaScript
typescript
// 1. Listen for Broadcasts
channel.on('broadcast', { event: 'message' }, (payload) => {
console.log('Received broadcast payload:', payload);
});
// 2. Commit the subscription
channel.subscribe();
// 3. Send a Broadcast
channel.send('broadcast', { event: 'message', payload: { text: 'Hello!' } });Python
python
# 1. Listen for Broadcasts
def on_message(payload): print(payload)
channel.on_broadcast("message", callback=on_message)
# 2. Commit the subscription
channel.subscribe()
# 3. Send a Broadcast
channel.send_broadcast("message", {"text": "Hello!"})Dart / Flutter
dart
// 1. Listen for Broadcasts
channel.onBroadcast('message', (payload) { print(payload); });
// 2. Commit the subscription
channel.subscribe();
// 3. Send a Broadcast
channel.sendBroadcast('message', {'text': 'Hello!'});03
Listen for Database Changes
Receive real-time events for table operations (INSERT, UPDATE, DELETE).
JavaScript
typescript
channel.on('postgres_changes', {
event: 'INSERT',
schema: 'public',
table: 'messages'
}, (payload) => {
console.log('New database message:', payload.new);
});Python
python
channel.on_postgres_changes(
event="INSERT",
schema="public",
table="messages",
callback=lambda payload: print(payload)
)Dart / Flutter
dart
channel.onPostgresChanges(
event: 'INSERT',
schema: 'public',
table: 'messages',
callback: (payload) => print(payload),
);Realtime updates are **secure by default**. Our engine automatically honors your PostgreSQL Row Level Security (RLS) policies, ensuring users only receive data they are authorized to see.
Presence Tracking
Track user online status and synchronize metadata across instances. Perfect for "Who's online" lists or shared cursors in collaborative tools.
JavaScript / TypeScript
typescript
// Listen for presence state changes
channel.on('presence', { event: 'sync' }, () => {
const presenceState = channel.presenceState();
console.log('Online users:', presenceState);
});
channel.subscribe(async (status) => {
if (status === 'SUBSCRIBED') {
await channel.track({ online_at: new Date().toISOString() });
}
});Python
python
# Register Presence Sync
channel.on_presence_sync(callback=lambda state: print(state))
channel.subscribe()
# Track presence
channel.track({"user": "python_client", "status": "online"})Dart / Flutter
dart
// Register Presence Sync
channel.onPresenceSync((state) { print(state); });
channel.subscribe();
// Track presence
channel.track({'user': 'dart_client', 'status': 'online'});