Supabase.Realtime.Channel
(supabase_realtime v0.5.0)
Copy Markdown
Represents a subscription channel for Supabase Realtime events.
A channel narrows the scope of data flow to subscribed clients. Each channel is created with a topic and can hold multiple event bindings.
Broadcast Options
When creating a channel, you can pass broadcast options:
broadcast: [self: true]- Receive your own broadcast messages on this channel. By default, the server does not echo your broadcasts back to you.broadcast: [ack: true]- Enable delivery confirmation for broadcasts. Usebroadcast_with_ack/3andwait_for_ack/2to send and confirm.broadcast: [replay: [since: epoch, limit: n]]- Enable message replay for missed broadcasts. Only available on private channels. Thesincevalue is a Unix epoch timestamp (integer) andlimitis the max number of messages to replay (server max: 25).
Presence
presence: [key: "user:123"]- Set a custom presence key for this channel. This key identifies the client in presence state maps.
Wildcard Events
You can subscribe to all events of a given type using event: :all or
event: "*". This works for both broadcast and postgres_changes bindings.
Realtime.on(channel, "broadcast", event: "*")
Realtime.on(channel, "postgres_changes", event: :all, schema: "public")Example
{:ok, channel} = MyApp.Realtime.channel("room:lobby",
broadcast: [self: true, ack: true],
presence: [key: "user:42"]
)
# With message replay on a private channel
{:ok, channel} = MyApp.Realtime.channel("private:room",
broadcast: [replay: [since: 1_700_000_000, limit: 25]],
private: true
)
Summary
Functions
Checks if acknowledgments are enabled for this channel.
Adds a binding for an event type.
Adds a pending acknowledgment to the channel.
Checks if a push can be sent on this channel.
Checks if the channel is closed.
Checks if the channel is in an error state.
Gets the caller process for a pending acknowledgment.
Checks if the channel is joined (subscribed).
Checks if the channel is in the process of joining.
Checks if the channel is in the process of leaving.
Creates a new channel struct.
Removes bindings for an event type.
Removes a pending acknowledgment from the channel.
Sets the join reference for the channel.
Updates the channel state.
Types
@type t() :: %Supabase.Realtime.Channel{ bindings: map(), join_ref: Supabase.Realtime.ref() | nil, params: map(), pending_acks: map(), ref: String.t(), registry: pid() | atom(), state: Supabase.Realtime.channel_state(), timeout: pos_integer(), topic: String.t() }
Channel structure for Supabase Realtime subscriptions.
Fields:
topic- The channel topic stringregistry- The channel registry process or modulebindings- Event bindings for this channelstate- Current state of the channeljoin_ref- Reference for the join messagetimeout- Timeout for operations in millisecondsparams- Additional parameters for the channelref- Unique reference for this channelpending_acks- Map of pending acknowledgment references and their details
Functions
Checks if acknowledgments are enabled for this channel.
Parameters
channel- The channel struct
Returns
boolean- True if acknowledgments are enabled, false otherwise
Adds a binding for an event type.
Parameters
channel- The channel structtype- The event typefilter- The event filtercallback- Optional callback function
Returns
%Channel{}- Updated channel struct
Adds a pending acknowledgment to the channel.
Parameters
channel- The channel structack_ref- The acknowledgment referencecaller- The process that will receive the acknowledgment
Returns
%Channel{}- Updated channel struct
Checks if a push can be sent on this channel.
A push can be sent when the channel is joined and the client is connected.
Parameters
channel- The channel struct
Returns
boolean- True if a push can be sent, false otherwise
Checks if the channel is closed.
Parameters
channel- The channel struct
Returns
boolean- True if closed, false otherwise
Checks if the channel is in an error state.
Parameters
channel- The channel struct
Returns
boolean- True if errored, false otherwise
Gets the caller process for a pending acknowledgment.
Parameters
channel- The channel structack_ref- The acknowledgment reference
Returns
{:ok, pid()}- The caller process:error- Acknowledgment not found
Checks if the channel is joined (subscribed).
Parameters
channel- The channel struct
Returns
boolean- True if joined, false otherwise
Checks if the channel is in the process of joining.
Parameters
channel- The channel struct
Returns
boolean- True if joining, false otherwise
Checks if the channel is in the process of leaving.
Parameters
channel- The channel struct
Returns
boolean- True if leaving, false otherwise
Creates a new channel struct.
Parameters
topic- The topic to subscribe toregistry- The channel registry module or PIDopts- Additional options for the channel
Options
:timeout- Timeout for operations in milliseconds (default: 10000):params- Additional parameters for the channel:ref- Optional reference for the channel
Returns
%Channel{}- A channel struct
Removes bindings for an event type.
Parameters
channel- The channel structtype- The event typefilter- The event filter
Returns
%Channel{}- Updated channel struct
Removes a pending acknowledgment from the channel.
Parameters
channel- The channel structack_ref- The acknowledgment reference
Returns
%Channel{}- Updated channel struct
@spec set_join_ref(t(), Supabase.Realtime.ref()) :: t()
Sets the join reference for the channel.
Parameters
channel- The channel structjoin_ref- The join reference
Returns
%Channel{}- Updated channel struct
@spec update_state(t(), Supabase.Realtime.channel_state()) :: t()
Updates the channel state.
Parameters
channel- The channel structstate- The new state
Returns
%Channel{}- Updated channel struct