Appwrite.Types.Client.Realtime (appwrite v1.0.0)

View Source

Struct representing the state of an Appwrite Realtime WebSocket connection.

Holds all mutable state needed to manage the connection lifecycle: the underlying socket, subscribed channels, active subscriber callbacks, reconnection bookkeeping, and the last received server message.

Fields

  • socket (:gen_tcp.socket() | nil) — The active WebSocket socket, or nil when disconnected.

  • timeout (non_neg_integer() | nil) — Operation timeout in milliseconds. Defaults to 5_000 when nil.

  • url (String.t() | nil) — WebSocket endpoint URL, or nil before the client is configured.

  • last_message (Appwrite.Types.Client.RealtimeResponse.t() | nil) — The most recent message received from the server.

  • channels (MapSet.t(String.t())) — Set of channel names the client is currently subscribed to.
  • subscriptions (%{required(non_neg_integer()) => subscription()}) — Map of subscription ID to subscription descriptor. Each descriptor holds the channel list and the event callback.
  • subscriptions_counter (non_neg_integer()) — Monotonically increasing counter used to generate unique subscription IDs.
  • reconnect (boolean()) — Whether the client should attempt to reconnect automatically after a disconnection.
  • reconnect_attempts (non_neg_integer()) — Number of reconnection attempts made since the last successful connection.

Subscription descriptor shape

%{
  channels: [String.t()],
  callback: (Appwrite.Types.Client.RealtimeResponseEvent.t() -> any())
}

Summary

Functions

Removes the given channels from the struct's channel set.

Returns the configured timeout in milliseconds.

Returns a new Realtime struct with default values.

Types

subscription()

@type subscription() :: %{
  channels: [String.t()],
  callback: (Appwrite.Types.Client.RealtimeResponseEvent.t() -> any())
}

t()

@type t() :: %Appwrite.Types.Client.Realtime{
  channels: MapSet.t(String.t()),
  last_message: Appwrite.Types.Client.RealtimeResponse.t() | nil,
  reconnect: boolean(),
  reconnect_attempts: non_neg_integer(),
  socket: :gen_tcp.socket() | nil,
  subscriptions: %{required(non_neg_integer()) => subscription()},
  subscriptions_counter: non_neg_integer(),
  timeout: non_neg_integer() | nil,
  url: String.t() | nil
}

Functions

clean_up(realtime, channels)

@spec clean_up(t(), [String.t()]) :: t()

Removes the given channels from the struct's channel set.

Does not close any WebSocket connection or notify the server; callers are responsible for resubscribing if needed.

Examples

iex> rt = Realtime.new(channels: MapSet.new(["files", "documents"]))
iex> Realtime.clean_up(rt, ["files"])
%Realtime{channels: MapSet.new(["documents"]), ...}

get_timeout(realtime)

@spec get_timeout(t()) :: non_neg_integer()

Returns the configured timeout in milliseconds.

Falls back to 5_000 ms when timeout is nil.

Examples

iex> Realtime.get_timeout(%Realtime{timeout: nil})
5000

iex> Realtime.get_timeout(%Realtime{timeout: 10_000})
10000

new(attrs \\ [])

@spec new(Enum.t()) :: t()

Returns a new Realtime struct with default values.

Optionally accepts a keyword list or map of field overrides. Only known struct keys are accepted; unknown keys raise a KeyError.

Examples

iex> Appwrite.Types.Client.Realtime.new()
%Appwrite.Types.Client.Realtime{channels: MapSet.new(), ...}

iex> Appwrite.Types.Client.Realtime.new(url: "wss://cloud.appwrite.io/v1/realtime")
%Appwrite.Types.Client.Realtime{url: "wss://...", ...}