Supabase.Realtime.Channel.Registry
(supabase_realtime v0.5.0)
Copy Markdown
Registry for managing Realtime channel subscriptions.
Tracks active channels and their subscriptions, and routes incoming messages to the appropriate callback functions.
Message Routing
When a message arrives, the registry matches it against channel bindings:
- Broadcast events are matched by event name. Bindings with
event: "*"(wildcard) match every broadcast event on the channel. - Postgres changes are matched by server-assigned binding IDs. After a channel joins, the server reply includes IDs for each postgres_changes binding. The registry stores these IDs so that incoming database events are dispatched only to the correct bindings.
- Presence events are dispatched to all channels subscribed to the topic.
Connection State Notifications
When the WebSocket connection state changes (for example, from :open to
:closed), the connection process notifies the registry. The registry then
dispatches a {:connection, :state_change, %{old: old, new: new}} event
to all registered callback modules.
Postgres Type Transforms
Database change payloads include column metadata. The registry uses
Supabase.Realtime.PostgresTypes to convert string values in record
and old_record maps to native Elixir types before dispatching events.
Summary
Functions
Returns a specification to start this module under a supervisor.
Creates a new channel in the registry.
Handles a message from the server.
Lists all active channels.
Removes all channel subscriptions.
Starts the channel registry process.
Subscribes to events on a channel.
Unsubscribes from a channel.
Types
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec create_channel(GenServer.server(), String.t(), keyword()) :: {:ok, Supabase.Realtime.Channel.t()} | {:error, term()}
Creates a new channel in the registry.
Parameters
server- The server PID or nametopic- The topic to subscribe toopts- Channel options
@spec handle_message(GenServer.server(), Supabase.Realtime.realtime_message()) :: :ok
Handles a message from the server.
Parameters
server- The server PID or namemessage- The message to handle
@spec list_channels(GenServer.server()) :: [Supabase.Realtime.Channel.t()]
Lists all active channels.
Parameters
server- The server PID or name
@spec remove_all_channels(GenServer.server()) :: :ok | {:error, term()}
Removes all channel subscriptions.
Parameters
server- The server PID or name
@spec start_link(keyword()) :: GenServer.on_start()
Starts the channel registry process.
Options
:name- Optional registration name:module- The callback module for event handling
@spec subscribe( GenServer.server(), Supabase.Realtime.Channel.t(), String.t(), map() | keyword() ) :: :ok | {:error, term()}
Subscribes to events on a channel.
Parameters
server- The server PID or namechannel- The channel structtype- The event typefilter- The event filter
@spec unsubscribe(GenServer.server(), Supabase.Realtime.Channel.t()) :: :ok | {:error, term()}
Unsubscribes from a channel.
Parameters
server- The server PID or namechannel- The channel to unsubscribe from