Skip to content

Real-time Updates

Overview

Harmony uses Supabase Realtime (built on PostgreSQL's replication) for live data delivery. The RealtimeConnectionManager service provides a resilient wrapper with automatic reconnection and status tracking.

Subscription Architecture

Subscribing to Tables

RealtimeConnectionManager provides typed subscription methods:

Subscription Types

MethodUse Case
subscribeToTable()Listen for INSERT, UPDATE, DELETE on a single table
subscribeToMultipleTables()Multiple tables on one channel
subscribe()Legacy single-event subscription

Filtering

Subscriptions can filter by column values to receive only relevant events:

typescript
manager.subscribeToTable('chat-messages', 'messages', {
  filter: `channel_id=eq.${channelId}`,
  event: 'INSERT',
  callback: (payload) => handleNewMessage(payload.new)
})

Connection Lifecycle

Per-Channel Status

Each channel tracks its own connection status:

StatusMeaning
disconnectedNot subscribed
connectingInitial connection attempt
connectedActively receiving events
reconnectingConnection lost, attempting recovery
errorFailed after max retries

Reconnection Strategy

Backoff Parameters

ParameterValue
Base delay1 second
Max delay30 seconds
Factor2x
Max retries10
Jitter20%

Rapid Close Detection

If 3 connections close within a short period, the manager backs off for 30 seconds to avoid overwhelming the server.

Auth Integration

On SIGNED_OUT, all realtime subscriptions are cleaned up immediately.

Presence

User presence is tracked through Supabase Realtime presence channels:

Presence States

StatusVisible to OthersReceives Notifications
OnlineYesYes
AwayYes (as away)Yes
Do Not DisturbYes (as DND)Suppressed
InvisibleNo (appears offline)Yes

Key Realtime Channels

ChannelTable(s)Purpose
Chat messagesmessagesNew/edited/deleted messages in a channel
DM messagesmessagesDirect message delivery
NotificationsnotificationsReal-time notification delivery
Typingtyping_indicatorsTyping status in channels/DMs
PresenceRealtime presenceOnline/offline status
Encryption keysmegolm_session_sharesKey sharing for E2EE
Reactionsmessage_reactionsMessage reaction updates

Monitoring

typescript
const manager = RealtimeConnectionManager.getInstance()

// Get debug info for all channels
const debug = manager.getDebugInfo()

// Monitor specific channel status
manager.onStatusChange('my-channel', (status) => {
  console.log('Status:', status)
})

// Get current status
const status = manager.getStatus('my-channel')

Health Check

A 60-second health check timer monitors long-lived error states:

  • If a channel has been in error status for more than 3 minutes, it resets and attempts reconnection
  • This handles cases where the network recovers but the subscription wasn't automatically restored

See also: Chat Message Flow for how messages use realtime, and Authentication Flow for session management.

Released under the AGPL-3.0 License.