Skip to content

channel / realtime ​

Reference for Supabase Realtime in the schema. For the walkthrough, see the Realtime guide.

realtime (table option) ​

ts
type TableRealtime = boolean | { replicaIdentity?: 'default' | 'full' };
ValueGenerated SQL (realtime.sql)SDK
trueALTER PUBLICATION supabase_realtime ADD TABLE public.<table> (if missing), REPLICA IDENTITY DEFAULTclient.<table>.subscribe(...)
{ replicaIdentity: 'full' }Same, with REPLICA IDENTITY FULLclient.<table>.subscribe(...)
falseALTER PUBLICATION supabase_realtime DROP TABLE public.<table> (if present)none
left outnothingnone

Works in manual and Database mode. Without the supabase_realtime publication, push prints a warning and changes nothing.

channel ​

ts
// exported by the accessnexus package
function channel(def: ChannelDef): ChannelDef;

interface ChannelDef {
  topic: string;                // 'lobby' or 'room:{id}'
  table?: string;               // the {param} is this table's primary key
  broadcast?: ChannelAccess;
  presence?: ChannelAccess;
}

interface ChannelAccess {
  receive?: ChannelRule;        // SELECT on realtime.messages
  send?: ChannelRule;           // INSERT on realtime.messages
}

type ChannelRule = boolean | PolicyRule;

Pass channels to defineSchema(tables, { channels: { <name>: channel({ ... }) } }). Names are lowercase snake_case, up to 40 characters.

OptionDescription
topicA fixed topic, or a prefix plus one trailing {param}. Letters, digits and :_.-. A topic that is only {param} is rejected (it would match every channel).
tableRequires a {param}. Every rule then also checks EXISTS (SELECT 1 FROM public.<table> WHERE <pk> = <param>) as the user, so the table's SELECT policy decides. uuid and integer keys are matched without a cast that could fail; a malformed parameter matches nothing.
broadcast.receiveReceive broadcast messages.
broadcast.sendSend broadcast messages.
presence.receiveSee the presence state.
presence.sendTrack your own presence.

Rules ​

RulePolicy roleCondition
trueauthenticated(SELECT auth.uid()) IS NOT NULL
false, omitted, policy.none()noneno policy (an existing one is dropped)
policy.isAuthenticated()authenticated(SELECT auth.uid()) IS NOT NULL
policy.public()publicTRUE
policy.hasRole(role)authenticatedapp_metadata.role claim equals role
policy.custom(sql)publicsql, with {param} replaced by the topic parameter (text)
policy.any / policy.allwidest / narrowest of the partsOR / AND

Every condition is also ANDed with the extension (realtime.messages.extension = 'broadcast' | 'presence'), the topic match and, with table, the row check.

Generated policies ​

One policy per granted extension and action, on realtime.messages:

sql
CREATE POLICY "accessnexus_rt_project_room_broadcast_receive"
  ON realtime.messages FOR SELECT
  TO authenticated
  USING (
    realtime.messages.extension = 'broadcast'
    AND left(realtime.topic(), 8) = 'project:'
    AND length(realtime.topic()) > 8
    AND (SELECT auth.uid()) IS NOT NULL
    AND EXISTS (SELECT 1 FROM public.projects WHERE projects.id = CASE WHEN substr(realtime.topic(), 9) ~* '^[0-9a-f]{8}-...$' THEN substr(realtime.topic(), 9)::uuid END)
  );

Names are <policyPrefix>_rt_<channel>_<broadcast|presence>_<receive|send>. Before creating them, realtime.sql drops every policy on realtime.messages whose name starts with <policyPrefix>_rt_ and is no longer declared. Other policies are never touched.

SDK ​

AccessorReturns
client.<table>.subscribe(listener, { event?, filter?, onStatus? })() => void (unsubscribe). Only on realtime: true tables.
client.channels.<name>(param) / client.channels.<name>()The provider's RealtimeChannel, private, for the topic.
q.<table>.invalidateOnChange({ event?, filter? })() => void. With reactQuery: true; needs queryClient.

Types exported by client.ts: RealtimeEvent, RealtimeChange<Row>, RealtimeChangeOf<Row, Event>, RealtimeFilter<Row>, RealtimeSubscribeOptions<Row, Event>, ChannelOf<TClient>. See Generated SDK.

A provider without channel / removeChannel (a test double, for example) makes subscribe and channels.* throw [AccessNexus] ... needs a Supabase client with Realtime.

Released under the MIT License.