How to Implement Real-Time Features with Supabase Realtime Subscriptions
๐ Want the best deal? Check current prices and availability.
Compare Prices โWhen you buy through links on our site, we may earn a commission.
Real-time features are a trap for solo developers. Not because they're hard to build โ they're hard to build correctly. You stand up a WebSocket server in an afternoon, then spend three weeks adding auth, per-row permission checks, reconnect handling, presence tracking, and a message fan-out strategy that doesn't melt under load. By the time you're done, you've built a second backend you have to maintain.
Supabase Realtime is an attempt to skip that. It gives you three primitives on top of the Postgres database you already have, and the permissions model is row-level security โ the same policies you wrote for your REST endpoints. That's the pitch. Whether it holds up depends heavily on which of the three primitives you pick, because one of them is significantly better than the others at scale.
This tutorial walks through all three, with working code, real limits, and an honest look at where things break.
The Three Primitives
Most people say "Supabase Realtime" and mean one thing. It's actually three distinct products sharing a single WebSocket connection:
| Primitive | What it does | Backed by |
|---|---|---|
| Postgres Changes | Streams INSERT/UPDATE/DELETE events for a table via logical replication | Your database's WAL |
| Broadcast | Fire-and-forget messages between clients, or from a database trigger | Ephemeral in-memory pub/sub |
| Presence | Shared state that syncs on join/leave โ "who's online," cursors, typing indicators | Ephemeral, tied to connection lifecycle |
One client, one socket, many channels. That detail matters later when you're debugging.
Setup
Install the client:
npm install @Supabase/supabase-js
import { createClient } from '@supabase/supabase-js'
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_ANON_KEY
)
For Postgres Changes, you have to explicitly opt each table into the replication publication. Either flip the toggle in Table Editor โ Realtime, or run:
alter publication supabase_realtime add table public.messages;
Broadcast and Presence need no database setup at all โ they never touch Postgres by default.
Part 1: Postgres Changes
Let's build a chat feed. You have a messages table and you want new rows to appear for everyone in a room.
const channel = supabase
.channel('messages-room-42')
.on(
'postgres_changes',
{
event: 'INSERT',
schema: 'public',
table: 'messages',
filter: 'room_id=eq.42',
},
(payload) => {
console.log('new row:', payload.new)
}
)
.subscribe((status, err) => {
console.log(status) // SUBSCRIBED | CHANNEL_ERROR | TIMED_OUT | CLOSED
})
// later
await supabase.removeChannel(channel)
Three things trip people up here.
Filters are single-column only. `filter: 'room_id=
๐ Want the best deal? Check current prices and availability.
Compare Prices โ