Supabase.Realtime.Message (supabase_realtime v0.5.0)

Copy Markdown

Message encoding and decoding for Supabase Realtime.

This module handles serialization and deserialization of the WebSocket messages exchanged with the Supabase Realtime service.

Summary

Functions

Constructs an access token update message.

Constructs a broadcast message.

Constructs a broadcast message with acknowledgment support.

Decodes a message received from the Supabase Realtime server.

Encodes a message for sending to the Supabase Realtime server.

Constructs a heartbeat message.

Constructs a presence tracking message.

Constructs a presence untrack message.

Constructs a subscription message for a channel.

Constructs an unsubscribe message for a channel.

Functions

access_token_message(channel, token)

@spec access_token_message(Supabase.Realtime.Channel.t(), String.t()) :: map()

Constructs an access token update message.

Parameters

  • channel - The channel struct
  • token - The new access token

Returns

  • map - Access token message payload

broadcast_message(channel, event, payload)

@spec broadcast_message(Supabase.Realtime.Channel.t(), String.t(), map()) :: map()

Constructs a broadcast message.

Parameters

  • channel - The channel struct
  • event - The event name
  • payload - The message payload

Returns

  • map - Broadcast message payload

broadcast_message_with_ack(channel, event, payload, ack_ref)

@spec broadcast_message_with_ack(
  Supabase.Realtime.Channel.t(),
  String.t(),
  map(),
  String.t()
) :: map()

Constructs a broadcast message with acknowledgment support.

Parameters

  • channel - The channel struct
  • event - The event name
  • payload - The message payload
  • ack_ref - The acknowledgment reference

Returns

  • map - Broadcast message payload with acknowledgment reference

decode(data)

@spec decode(binary()) :: {:ok, map()} | {:error, term()}

Decodes a message received from the Supabase Realtime server.

Parameters

  • data - The raw message data

Returns

  • {:ok, map} - Decoded message
  • {:error, term} - Error information

Examples

iex> Message.decode("{"topic":"realtime:public","event":"INSERT","payload":{},"ref":"1"}")
{:ok, %{"topic" => "realtime:public", "event" => "INSERT", "payload" => %{}, "ref" => "1"}}

encode(message)

@spec encode(Supabase.Realtime.realtime_message() | map()) ::
  {:ok, binary()} | {:error, term()}

Encodes a message for sending to the Supabase Realtime server.

Parameters

  • message - The message to encode

Returns

  • binary - JSON-encoded message

Examples

iex> Message.encode(%{topic: "realtime:public", event: "INSERT", payload: %{}, ref: "1"})
"{"topic":"realtime:public","event":"INSERT","payload":{},"ref":"1"}"

heartbeat_message()

@spec heartbeat_message() :: map()

Constructs a heartbeat message.

Returns

  • map - Heartbeat message payload

presence_track_message(channel, presence_state)

@spec presence_track_message(Supabase.Realtime.Channel.t(), map()) :: map()

Constructs a presence tracking message.

Parameters

  • channel - The channel struct
  • presence_state - The presence state to track

Returns

  • map - Presence track message payload

presence_untrack_message(channel)

@spec presence_untrack_message(Supabase.Realtime.Channel.t()) :: map()

Constructs a presence untrack message.

Parameters

  • channel - The channel struct

Returns

  • map - Presence untrack message payload

subscription_message(channel)

@spec subscription_message(Supabase.Realtime.Channel.t()) :: map()

Constructs a subscription message for a channel.

Parameters

  • channel - The channel struct

Returns

  • map - Subscription message payload

unsubscribe_message(channel)

@spec unsubscribe_message(Supabase.Realtime.Channel.t()) :: map()

Constructs an unsubscribe message for a channel.

Parameters

  • channel - The channel struct

Returns

  • map - Unsubscribe message payload