6. Realtime: Live Data & Presence
Building Live Applications Without Polling
Supabase Realtime is an Elixir/Phoenix-based WebSocket server that turns your Postgres database into a live data stream. Instead of polling an endpoint every few seconds, clients subscribe to changes and receive them instantly as they happen — sub-100ms latency from database write to client delivery.
Realtime provides three capabilities: Postgres Changes, Broadcast, and Presence. Postgres Changes subscriptions listen to database changes using PostgreSQL's logical replication system. This means you get database-level consistency — changes are delivered exactly as they're committed. Broadcast is for ephemeral messages that don't need to be persisted — think cursor positions, typing indicators, or game state. Presence tracks which users are online in a channel — perfect for chat rooms, collaborative editing, or live dashboards. All three capabilities use WebSockets for low-latency communication. Realtime is production-tested at scale, handling millions of concurrent connections. Understanding these capabilities lets you build truly live applications that feel responsive and immediate.
Three Realtime Capabilities
Postgres Changes subscriptions are the most powerful feature. You can subscribe to INSERT, UPDATE, or DELETE events on specific tables or even specific rows using filters. The WAL is the most reliable way to detect changes — it captures every modification to your database. Broadcast is for ephemeral events that don't need database persistence. It's lower latency because it skips the database entirely. Presence is built on top of Broadcast and provides a way to track user state in a channel. Each user publishes a state object, and all members see the live list of who's present and their metadata. Choose the right capability for your use case. Use Postgres Changes for anything that's persisted. Use Broadcast for ephemeral events. Use Presence for tracking user state.
Subscribe to INSERT, UPDATE, DELETE events on specific tables or rows. Powered by WAL (Write-Ahead Log) logical decoding.
Send ephemeral messages between connected clients via a named channel. Not persisted. Perfect for presence signals and cursor positions.
Track which users are online in a channel. Each user publishes a state object; all members see the live list of who's present and their metadata.
Subscribing to Database Changes
To subscribe to database changes, create a channel and add a postgres_changes event handler. The handler receives a payload with eventType, new record, and old record. You can filter by schema, table, and row using the filter option. This allows you to subscribe only to changes that matter to your application. For example, you can subscribe only to INSERT events for messages in a specific room. Always clean up subscriptions when your component unmounts — use supabase.removeChannel(channel) to prevent memory leaks and connection issues. Realtime respects RLS policies — clients only receive events for rows they have permission to see. This is critical for security. Enable RLS on any table you subscribe to; otherwise all changes are broadcast to all subscribers.
// Subscribe to all changes on the 'messages' table const channel = supabase .channel('public:messages') .on( 'postgres_changes', { event: '*', schema: 'public', table: 'messages' }, (payload) => { console.log('Change:', payload.eventType, payload.new, payload.old); } ) .subscribe();
Presence: Track Online Users
Presence tracking is built on top of channels. Each user has a unique key (usually their user ID) and can publish a state object. The state can include anything — username, avatar URL, cursor position, online status. When a user connects, their state is published to all members of the channel. When a user disconnects, their state is automatically removed. You can listen to the sync event to get the full presence state of all users. This is perfect for chat rooms, collaborative editing, or any application where you need to show who's online. The presence state is automatically synchronized across all connected clients — no manual polling needed. Use presence to build features like 3 users currently viewing this page or Jane is typing....
const presenceChannel = supabase.channel('room:42', { config: { presence: { key: user.id } } }); // Track state for the current user await presenceChannel.track({ user_id: user.id, username: user.email, cursor: { x: 0, y: 0 }, online_at: new Date().toISOString() });
Broadcast: Ephemeral Messaging
Broadcast is for ephemeral messages that don't need to be persisted. Think of it like a chat room without history — it's for real-time signals, not permanent data. Broadcast messages are not stored in the database. They're sent directly from client to client through the Realtime server. This makes Broadcast much lower latency than Postgres Changes because it skips the database entirely. Use Broadcast for high-frequency events like cursor positions, typing indicators, or game actions. The send method is called on a channel with a type and payload. The payload can be any JSON-serializable data. Listeners are registered with .on(broadcast, { event: event_name }, handler). This pattern is perfect for collaborative features where low latency is critical.
// Sender: broadcast an event to all channel members await supabase .channel('collab:doc-123') .send({ type: 'broadcast', event: 'cursor_moved', payload: { user_id: user.id, x: 450, y: 230 } });
Broadcast vs Postgres Changes
Use Broadcast for ephemeral, high-frequency events (cursor moves, typing indicators, game state) that don't need to be persisted. Use Postgres Changes for anything that IS persisted to the database (new messages, updated orders). Broadcast has much lower latency because it skips the database entirely.
Knowledge Check
Ready to test your understanding of 6. Realtime: Live Data & Presence?