BaileysEx.Connection.Socket (baileys_ex v0.1.0-alpha.8)

Copy Markdown View Source

Connection state machine for the WebSocket and Noise transport lifecycle.

The socket mirrors Baileys rc.9's makeSocket boundary: transport startup, Noise handshake, post-handshake frame IO, connection updates, keep-alive, unified session startup, routing updates, and explicit logout.

Summary

Functions

Supervisor child specification override.

Commands the socket to establish a connection.

Disconnects the active connection.

Logs out from the server and closes the connection.

Performs an IQ query cycle and awaits the node response.

Requests a pairing code for a companion device linking flow.

Sends an outbound binary node to the server.

Bypass the protocol and send a raw binary payload.

Pushes a presence update to the network.

Sends the post-open unified session telemetry node.

Send a WAM analytics buffer through the w:stats IQ path.

Exports a snapshot representation of the socket.

Starts the Socket GenStatem process.

Starts an IQ query cycle without waiting for the response.

Starts an IQ query cycle without waiting for the response and routes the eventual {__MODULE__, ref, result} message to reply_to.

Returns the internal state of the state machine.

Types

presence_type()

@type presence_type() :: :available | :unavailable

snapshot()

@type snapshot() :: %{
  state: state(),
  retry_count: non_neg_integer(),
  buffer_size: non_neg_integer(),
  transport_connected?: boolean(),
  last_error: term()
}

state()

@type state() ::
  :disconnected | :connecting | :noise_handshake | :authenticating | :connected

t()

@type t() :: %BaileysEx.Connection.Socket{
  auth_state: term(),
  buffer: binary(),
  clock_ms_fun: (-> integer()),
  config: BaileysEx.Connection.Config.t(),
  date_time_fun: (-> DateTime.t()),
  epoch: non_neg_integer(),
  event_emitter: GenServer.server() | nil,
  keep_alive_timer: reference() | nil,
  last_date_recv_at: integer() | nil,
  last_error: term(),
  message_tag_fun: (-> binary()),
  monotonic_ms_fun: (-> integer()),
  next_qr_timeout_ms: pos_integer(),
  noise: BaileysEx.Protocol.Noise.t() | nil,
  noise_opts: keyword(),
  pairing_iterations: pos_integer(),
  pending_lid: binary() | nil,
  pending_phone_pairing_notification:
    {binary(), BaileysEx.BinaryNode.t(), map()} | nil,
  pending_queries: %{optional(binary()) => {pid(), reference(), reference()}},
  qr_refs: [binary()],
  qr_timer: reference() | nil,
  retry_count: non_neg_integer(),
  server_time_offset_ms: integer(),
  signal_store: term(),
  task_supervisor: term(),
  transport_connected?: boolean(),
  transport_module: module(),
  transport_options: term(),
  transport_state: term() | nil
}

Functions

child_spec(opts)

@spec child_spec(keyword()) :: Supervisor.child_spec()

Supervisor child specification override.

connect(server)

@spec connect(GenServer.server()) :: :ok | {:error, {:invalid_state, state()}}

Commands the socket to establish a connection.

disconnect(server)

@spec disconnect(GenServer.server()) :: :ok

Disconnects the active connection.

logout(server)

@spec logout(GenServer.server()) :: :ok | {:error, :not_connected}

Logs out from the server and closes the connection.

query(server, node, timeout \\ 60000)

@spec query(GenServer.server(), BaileysEx.BinaryNode.t(), timeout()) ::
  {:ok, BaileysEx.BinaryNode.t()} | {:error, :not_connected | :timeout | term()}

Performs an IQ query cycle and awaits the node response.

request_pairing_code(server, phone_number, opts \\ [])

@spec request_pairing_code(GenServer.server(), binary(), keyword()) ::
  {:ok, binary()} | {:error, :not_connected | term()}

Requests a pairing code for a companion device linking flow.

send_node(server, node)

@spec send_node(GenServer.server(), BaileysEx.BinaryNode.t()) ::
  :ok | {:error, :not_connected | term()}

Sends an outbound binary node to the server.

send_payload(server, payload)

@spec send_payload(GenServer.server(), binary()) ::
  :ok | {:error, :not_connected | term()}

Bypass the protocol and send a raw binary payload.

send_presence_update(server, type)

@spec send_presence_update(GenServer.server(), presence_type()) ::
  :ok | {:error, :not_connected | term()}

Pushes a presence update to the network.

send_unified_session(server)

@spec send_unified_session(GenServer.server()) ::
  :ok | {:error, :not_connected | term()}

Sends the post-open unified session telemetry node.

send_wam_buffer(server, wam_buffer)

@spec send_wam_buffer(GenServer.server(), binary()) ::
  {:ok, BaileysEx.BinaryNode.t()} | {:error, :not_connected | :timeout | term()}

Send a WAM analytics buffer through the w:stats IQ path.

snapshot(server)

@spec snapshot(GenServer.server()) :: snapshot()

Exports a snapshot representation of the socket.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts the Socket GenStatem process.

start_query(server, node, timeout \\ 60000)

@spec start_query(GenServer.server(), BaileysEx.BinaryNode.t(), timeout()) ::
  {:ok, reference()} | {:error, :not_connected | :timeout | term()}

Starts an IQ query cycle without waiting for the response.

The caller will later receive {__MODULE__, ref, result}.

start_query(server, reply_to, node, timeout)

@spec start_query(GenServer.server(), pid(), BaileysEx.BinaryNode.t(), timeout()) ::
  {:ok, reference()} | {:error, :not_connected | :timeout | term()}

Starts an IQ query cycle without waiting for the response and routes the eventual {__MODULE__, ref, result} message to reply_to.

state(server)

@spec state(GenServer.server()) :: state()

Returns the internal state of the state machine.