Real-time Database (Supabase, Firebase)

A realtime database sends data changes to clients with no WebSocket server of your own. Supabase Realtime reads the Postgres WAL (the record of every change)…

So far you wrote the realtime server yourself: Socket.IO, rooms, the Redis adapter, sticky sessions. There is another road: let the database send the news. That is a realtime database, a database service that sends data changes to the clients subscribed to them, so you don't write a WebSocket server yourself. Supabase does it on top of the Postgres database. Firebase, a Google service, does it with its own document database.

It is as if the order book at Mrs. Rini's Laundry had a clerk who reads every new line in the shop's daily log, then phones everyone allowed to know: Mrs. Rini about every order, Mr. Darto only about his own. In this picture, the order book is a database table, the shop's daily log is Postgres's record of changes (the WAL, explained below), and the clerk is the Supabase Realtime server. The rule about who is allowed to know is RLS, also explained below.

An order status as a row change

In Supabase, the laundry board is just one Postgres table. A status change is an UPDATE of one row:

create table orders (
  id bigint primary key,
  name text, kg int, status text,
  customer uuid references auth.users
);
update orders set status = 'Ready for pickup' where id = 12;

The table has to be added to the publication supabase_realtime (the list of tables whose changes are broadcast). Then the client subscribes through a channel, a subscription line on the Supabase Realtime server:

import { createClient } from '@supabase/supabase-js' // 2.117.3

const supabase = createClient(SUPABASE_URL, SUPABASE_KEY) // a public key: fine in the browser, RLS is what guards the data

supabase
  .channel('order-12')
  .on('postgres_changes', { event: 'UPDATE', schema: 'public', table: 'orders', filter: 'id=eq.12' },
    (payload) => show(payload.new))
  .subscribe((status) => console.log(status)) // 'SUBSCRIBED', then news starts coming

This is the command that adds the orders table to the publication:

alter publication supabase_realtime add table orders;

Every UPDATE on row #12 reaches the callback as the object payload. new holds the row after the change:

{ schema: 'public', table: 'orders', commit_timestamp: '...', eventType: 'UPDATE',
  new: { id: 12, name: 'Mr. Darto', kg: 3, status: 'Ready for pickup', customer: '...' },
  old: { id: 12 }, errors: ... }

From the WAL to Mr. Darto's phone

Guess first: Eko changes #12's status through the Laravel app, with a plain UPDATE to Postgres, not through the Supabase library. Mr. Darto's phone is subscribed to changes on the orders table through supabase-js. Does his phone get the news?

Postgres writes every change to the WAL (Write-Ahead Log), the daily log used for recovery and replication (copying data to another database server). Supabase Realtime opens a logical replication slot, its own reading place in the WAL that remembers how far the WAL has been read. From there Supabase Realtime reads the changes, checks who may receive each one, then sends it over WebSocket. No polling of tables, and no trigger (a database function that runs by itself when data changes) for you to install.

Because the source is the WAL, changes from anywhere come through: Laravel, psql (the terminal program for Postgres), the Supabase dashboard, or a cron job (a scheduled task). So Mr. Darto's phone does get the news, even though Eko doesn't use the Supabase library.

RLS decides who gets what

RLS (Row Level Security) is Postgres's per-row rule: who may read which rows. Each RLS rule is called a policy. For Postgres Changes (the change subscription above), the select policy (the rule for reading) is what decides whose phone receives the news. This code turns RLS on for the orders table, then adds a policy. The policy lets a logged-in user read the orders whose pelanggan column holds their own id:

alter table orders enable row level security;

create policy "customers see their own orders" on orders
  for select to authenticated
  using (customer = (select auth.uid()));

Broadcast and Presence

The same channel has two more features:

Firebase: the other model

Firestore, the realtime database in Firebase, stores documents (objects made of fields, similar to JSON), not rows. Its subscription uses onSnapshot:

import { doc, onSnapshot } from 'firebase/firestore' // firebase 13.0.0

const stop = onSnapshot(doc(db, 'orders', '12'), (snap) => show(snap.data()))

What you trade

Checked in Supabase's docs on 11 October 2026:

Free Pro
Concurrent connections 200 500 (beyond that $10 per 1,000)
Messages per month 2 million 5 million (beyond that $2.50 per million)

One database change counts as one message per client listening to it. Mrs. Rini's Laundry: 30 changes a day to 5 screens = 150 messages a day, free. 10,000 phones subscribed to the whole table with no filter: one change = 10,000 messages, 300 changes a day = 90 million messages a month. With filter: 'id=eq.12', each change only reaches the order owner's phone and the staff screens.

What you get: no realtime server to run. What you give up: the API belongs to the vendor (the company that provides the service), security rests on correct RLS policies, and the bill grows with the number of listeners.

⚠️ News that stays silent, or leaks

TL;DR: A realtime database sends data changes to clients with no WebSocket server of your own. Supabase Realtime reads the Postgres WAL (the record of every change) through logical replication (the way Postgres shares its changes), so an UPDATE from anywhere (Laravel included) reaches .on('postgres_changes', { event, schema, table, filter }, cb) as { eventType, new, old, ... }. The table has to be in the supabase_realtime publication, the list of tables whose changes are broadcast. RLS (Postgres's rules on who may read which rows) decides who receives what, with one check per subscriber for each change. With no policy (RLS rule) everything stays silent, with no RLS everything leaks, and old holds only the primary key. Broadcast is for messages between clients and for large scale, Presence for who is online. Firebase uses documents and onSnapshot, with Security Rules that reject a query rather than filter its results. The cost is counted per message per listener: filter your subscriptions.

⚡ A database that does the telling

Write:

supabase
  .channel('order-12')
  .on('postgres_changes', { event: 'UPDATE', schema: 'public', table: 'orders', filter: 'id=eq.12' },
    (payload) => show(payload.new))
  .subscribe((status) => console.log(status)) // 'SUBSCRIBED', then news starts coming

Meaning: A realtime database sends data changes to the clients subscribed to them, so you don't write a WebSocket server yourself. A status change is an UPDATE of one row. Here supabase is the client from the Supabase library. This code subscribes to UPDATEs on the orders table, for row #12 only, and payload.new holds the row after the change.

⚡ The source is the WAL

Guess first: Mr. Darto's phone is subscribed as in the step before. Eko changes #12's status through Laravel, with a plain UPDATE to Postgres, not through the Supabase library. Does Mr. Darto's phone get the news?

It does. Supabase Realtime reads the changes from the WAL (Postgres's daily log of every change) through logical replication (the way Postgres shares its changes), so changes from anywhere come through.

Meaning: No polling of tables, and no trigger (a database function that runs by itself) for you to install.

⚡ RLS decides who gets what

Meaning: RLS (Row Level Security) is Postgres's rule on who may read which rows, and each of its rules is called a policy. The RLS select policy (the rule for reading) decides whose phone receives the news. RLS on with no policy: nobody receives anything, with no error. RLS off: every customer's orders leak.

What you will learn