Realtime: WebSocket Gateway, WebRTC, SSE, push
Developers · Realtime: WebSocket Gateway, WebRTC, SSE, pushRealtime: WebSocket Gateway, WebRTC, SSE, push
Extrovert provides a unified realtime system over WebSocket (/ws), Server-Sent Events (/api/v1/notifications/stream), and Web Push:
| Channel | For | Auth |
|---|---|---|
/ws — Gateway & Signaling |
Realtime events (feed, rooms, notifications), presence, 1:1 calls, voice channels, live DMs | Session cookie, Authorization: Bearer, ?token=, or message token |
/ws — push channel |
native/mobile call wake-ups | same |
GET /api/v1/notifications/stream |
notification SSE stream | Bearer (notifications) |
| Web Push (VAPID) | browser call notifications | subscription-based |
Realtime Gateway Multiplexing (/ws)
The WebSocket endpoint /ws functions as a full multiplexed event gateway for external and native clients.
Connecting & Authentication
Clients can authenticate through multiple methods:
- HTTP Handshake Header:
Authorization: Bearer <access_token_or_pat> - Query Parameter:
wss://host/ws?token=<access_token_or_pat>&auto_subscribe=1(supports OAuth tokens and Personal Access Tokensext_pat_...). Passingauto_subscribe=1automatically attaches the socket totimeline:home,notifications,presence, and all rooms the user is a member of. - In-Frame Authentication: Sockets can connect unauthenticated and provide their token in subscription, auth, or resume messages:
{ "action": "subscribe", "topic": "timeline:home", "token": "ext_pat_..." } - Session Cookie: Web browsers pass session cookies automatically.
Immediately after the socket opens, the server sends a handshake frame that tells you what you are connected to:
{
"type": "hello",
"protocol_version": 2,
"authenticated": true,
"user_id": "7",
"scopes": ["read", "notifications", "read:direct"],
"topics": ["timeline:home", "notifications", "presence", "mls", "room:<id>"],
"ring_size": 500,
"latest_seq": 104,
"server_time": 1760000000000
}
A session-authenticated socket reports scopes: null (a full credential).
Auto-Subscription & Room Lifecycle
Clients do not need to manually subscribe to each room individually. By passing auto_subscribe=1 in the connection URL or sending { "action": "auto_subscribe" } (or { "action": "resume", "auto_subscribe": true }), the server:
- Automatically subscribes the connection to
timeline:home,notifications,presence, and every room the user has joined. - Dynamically attaches new room topics whenever the user joins a room via REST (
POST /api/v1/rooms/:id/join). - Dynamically detaches room topics when the user leaves a room (
POST /api/v1/rooms/:id/leave).
Optimistic UI (client_id Echo)
When sending posts (POST /statuses), comments (POST /statuses/:id/comments), or room messages (POST /rooms/:id/channels/:cid/messages), clients can include an optional client_id (or client_tx_id / nonce). The server echoes this exact string back in both the HTTP response and the WebSocket broadcast event payload (msg.data.client_id). This allows clients to instantly transition pending local items into confirmed state with zero duplicate rendering.
Gateway Topics
Clients subscribe to specific topics using { "action": "subscribe", "topic": "<topic>" }:
| Topic | Events Broadcasted | Description |
|---|---|---|
timeline:home |
post_create, comment_create, post_delete |
Realtime home timeline updates |
room:<id> |
message_create, message_delete, member_join, member_leave, room_session_key, typing |
Room text and member events (membership enforced). room_session_key is legacy Megolm plumbing kept for older clients; MLS clients decrypt from their own group state. |
notifications |
notification_new |
Push notifications to active client |
presence |
user_online, user_offline, otk_low |
Mutual-follower presence, sequenced in the ring. The initial snapshot sent right after connect uses bare user_online frames (no seq). |
mls |
mls_epoch |
Group epoch advanced. Delivered only to that group's members; subscribe is gated on read:direct. |
Gateway Opcodes (Client → Server)
| Action | Payload | Description |
|---|---|---|
auto_subscribe |
{ "action": "auto_subscribe" } |
Automatically subscribe to feed, notifications, presence, and all joined rooms. |
subscribe |
{ "topic": "room:1", "token": "...", "auto_subscribe": true } |
Subscribe to an event topic (accepts topic, channel, or channels array). |
unsubscribe |
{ "topic": "room:1" } |
Unsubscribe from an event topic. |
resume |
{ "seq": 105, "auto_subscribe": true } |
Replay missed events since sequence number seq after a network drop. |
typing |
{ "channel": "room:1", "typing": true } |
Send a typing indicator to a room channel or direct chat. |
ping |
{ "action": "ping" } |
Keepalive heartbeat (server replies { "type": "pong" }). |
Gateway Events (Server → Client)
All broadcast events carry a strictly monotonic sequence number (seq) backed by a 500-event ring buffer on the server.
For legacy Megolm clients the server inlines a pending session_key directly in data when it has one for the message's group_session_id, so those clients decrypt without an extra round trip. MLS clients ignore the field — they decrypt from their own MLS group state:
{
"seq": 106,
"type": "gateway_event",
"topic": "room:1",
"event": "message_create",
"data": {
"id": "42",
"room_id": "1",
"channel_id": "1",
"author": { "id": "7", "username": "alice", "display_name": "Alice" },
"proto": "megolm",
"ciphertext": "...",
"group_session_id": "1",
"created_at": "2026-10-03T00:00:00.000Z",
"session_key": {
"key_id": 2,
"encrypted_key": "...",
"sender_id": "7"
}
}
}
When reconnecting after a disconnection, send { "action": "resume", "seq": 106, "auto_subscribe": true }.
You must re-subscribe (or pass auto_subscribe) before resume; replay is
filtered to the topics this socket is subscribed to, so a fresh socket with no
subscriptions legitimately replays zero frames. The server replies before
the replayed frames:
{
"type": "resumed",
"replayed": 2,
"last_seq": 108,
"oldest_seq": 12,
"truncated": false
}
followed by each missed event frame in order.
seq is gapless for ring-stored events: every value between oldest_seq and
last_seq corresponds to exactly one replayable frame. Some socket-targeted
frames (otk_low, per-socket notification pushes, room_session_key) omit
seq and are never replayed; they are delivered live only. truncated: true
means the 500-frame ring wrapped — full-rebootstrap rather than trusting a
partial replay.
Two things are never replayed:
- DMs (
new_dm,edit_dm,delete_dm) carry noseqand are not buffered. RefetchGET /api/v1/conversations/:usernameafter reconnecting. - Presence frames are unsequenced snapshots; refetch
GET /api/v1/calls/presencewhen you need certainty.
WebRTC Signaling (/ws)
In addition to the event gateway, /ws handles WebRTC 1:1 call signaling and room voice channels:
| Type | Payload | Meaning |
|---|---|---|
pong |
— | Reply to ping. |
push_registered |
— | Push channel accepted. |
user_online / user_offline |
{username, display_name} |
A mutual follower connected/disconnected (broadcast to mutuals). |
callee_available |
{to} |
Callee is online and free — proceed with the offer. |
user_busy |
{from} / {to} |
Callee in a call (or already has a pending call). |
calling_offline |
{to, expires_at} |
Callee offline; pending call queued (120 s TTL). |
user_offline |
{from} |
Call target unreachable. |
incoming_call |
{from, from_display, sdp?} |
Ring. With sdp it's a forwarded offer (1:1 or voice channel with channel_id). |
callee_ringing |
{to} |
Callee reconnected and is being rung. |
call_answered |
{from, from_display, sdp} |
Call accepted. |
call_ended |
{from} |
Peer hung up. |
call_declined |
{from} |
Peer declined. |
call_unanswered |
{from, to} |
Offline call timed out. |
new_dm |
{message: {…ciphertext…}, sender_curve, from_username, from_display} |
Live DM ciphertext to every open tab of the recipient. |
channel_joined |
{channel_id, self, members} |
Voice channel join ack. |
user_joined_channel / user_left_channel |
{channel_id, username, display_name?} |
Room voice-channel membership changes. |
call (push channel) |
{type:"call", from, from_display, cancel_token} |
Native call wake-up. |
missed_call (push channel) |
{type:"missed_call", from, from_display} |
Native missed-call notification. |
Voice-channel audio is peer-to-peer; the gateway only carries signaling. The speaking indicators are client-side: joining a channel turns the microphone on, each member's speaking state is derived locally from the audio level (no server event is involved), and the call bar shows the channel name plus a Mic on / Speaking / Muted chip.
Notification SSE
GET /api/v1/notifications/stream with Authorization: Bearer <token> (scope notifications):
event: connected
data: {}
id: 12
event: notification
data: {"id":12,"type":"like","actor_id":7,"post_id":21,"created_at":1750000000000}
: heartbeat
- Every notification frame carries
id:. SendLast-Event-ID: 12(or?last_event_id=12) on reconnect and the server replays notifications after that id from the database — this works across restarts, unlike the in-memory gateway ring. - One
notificationevent per new notification, pushed in-process the moment it's created (src/notif-broadcaster.js). - Heartbeat comment lines every 15 s keep proxies happy;
X-Accel-Buffering: nois set. - Close the connection to stop receiving.
Web Push (browsers)
-
Server must be configured with
VAPID_PUBLIC_KEY/VAPID_PRIVATE_KEY/VAPID_SUBJECT. -
The browser fetches
GET /api/v1/push/vapid-public, callspushManager.subscribe, and registers viaPOST /api/v1/push/subscribe({platform:"web", endpoint, p256dh, auth}). -
Payloads delivered by the server (via
web-push, urgency high, TTL 120):Payload When {type:"call", from, from_display, cancel_token}Incoming offline call → service worker shows a ringing notification with Answer / Decline actions. {type:"missed_call", from, from_display}The pending call timed out. -
Answer opens the chat page; the server rings on WS reconnect. Decline POSTs
{cancel_token}to/push/cancel-pending— no session needed, the unguessable token is the credential. -
Dead subscriptions (HTTP 400/404/410) are deleted automatically.
Native push channel
Instead of web-push, the native app's foreground service keeps a WebSocket open and sends push_register. The server delivers call / missed_call payloads over that socket (see above). This avoids third-party push relays entirely.
Presence API
GET /api/v1/calls/presence→ online mutual followers[{id, username, display_name, in_call}]GET /api/v1/calls/presence/:username→{online, in_call}
These read the in-memory signaling registry (getOnlineUsers / getUserPresence in src/webrtc-signaling.js).