{% extends "base.html" %} {% block title %}Real-time Features{% endblock %} {% block content %}
Demonstrates Live Queries, Presence Tracking, and Real-time Collection Updates
Tracks who's currently online in real-time
Subscribe to filtered real-time updates
Supports: field=value, field!=value, field>value, field<value
All events from collections in real-time. Create, update, or delete records to see live updates.
No events yet. Start by creating, updating, or deleting records.
// Connect to WebSocket
const ws = new WebSocket('ws://localhost:8000/api/v1/ws/realtime');
ws.onopen = () => {
// Authenticate (optional, for protected collections)
ws.send(JSON.stringify({ type: 'auth', token: 'YOUR_JWT_TOKEN' }));
// Subscribe to all collections
ws.send(JSON.stringify({ type: 'subscribe', collection: '*' }));
// Or a specific collection with filter
ws.send(JSON.stringify({ type: 'subscribe', collection: 'posts', filter: { status: 'published' } }));
};
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === 'record.created') console.log('New record:', msg.data);
if (msg.type === 'record.updated') console.log('Updated:', msg.data);
if (msg.type === 'record.deleted') console.log('Deleted:', msg.data);
};
import asyncio, json, websockets
async def listen():
uri = "ws://localhost:8000/api/v1/ws/realtime"
async with websockets.connect(uri) as ws:
# Authenticate
await ws.send(json.dumps({"type": "auth", "token": "YOUR_JWT_TOKEN"}))
# Subscribe to all events
await ws.send(json.dumps({"type": "subscribe", "collection": "*"}))
async for message in ws:
msg = json.loads(message)
print(f"Event: {msg['type']}, Data: {msg.get('data')}")
asyncio.run(listen())
# Get connection statistics
curl http://localhost:8000/api/v1/stats
# WebSocket endpoint
ws://localhost:8000/api/v1/ws/realtime
ws://localhost:8000/api/v1/ws/realtime?token=YOUR_JWT_TOKEN