WebsockexNova.Behaviors.ClientBehavior behaviour (WebsockexNova v0.1.1)

View Source

Behavior defining the contract for WebSocket client operations.

This behavior ensures a consistent API for WebSocket client operations across different implementations and enables proper mocking in tests.

Summary

Callbacks

Authenticates with the WebSocket server.

Closes the WebSocket connection.

Connects to a WebSocket server using the specified adapter.

Sends a ping message to the WebSocket server.

Registers a process to receive notifications from the connection.

Sends a raw WebSocket frame.

Sends a JSON message.

Sends a text message.

Gets the current connection status.

Subscribes to a channel or topic.

Unregisters a process from receiving notifications.

Unsubscribes from a channel or topic.

Callbacks

authenticate(conn, credentials, options)

@callback authenticate(
  conn :: WebsockexNova.ClientConn.t(),
  credentials :: map(),
  options :: map() | nil
) ::
  {:ok, WebsockexNova.ClientConn.t(), term()}
  | {:error, term()}
  | {:error, term(), WebsockexNova.ClientConn.t()}

Authenticates with the WebSocket server.

close(conn)

@callback close(conn :: WebsockexNova.ClientConn.t()) :: :ok

Closes the WebSocket connection.

connect(adapter, options)

@callback connect(adapter :: module(), options :: map()) ::
  {:ok, WebsockexNova.ClientConn.t()} | {:error, term()}

Connects to a WebSocket server using the specified adapter.

ping(conn, options)

@callback ping(conn :: WebsockexNova.ClientConn.t(), options :: map() | nil) ::
  {:ok, :pong} | {:error, term()}

Sends a ping message to the WebSocket server.

register_callback(conn, pid)

@callback register_callback(conn :: WebsockexNova.ClientConn.t(), pid :: pid()) ::
  {:ok, WebsockexNova.ClientConn.t()}

Registers a process to receive notifications from the connection.

send_frame(conn, frame)

@callback send_frame(conn :: WebsockexNova.ClientConn.t(), frame :: term()) ::
  :ok | {:error, term()}

Sends a raw WebSocket frame.

send_json(conn, data, options)

@callback send_json(
  conn :: WebsockexNova.ClientConn.t(),
  data :: map(),
  options :: map() | nil
) ::
  {:ok, term()} | {:error, term()}

Sends a JSON message.

send_text(conn, text, options)

@callback send_text(
  conn :: WebsockexNova.ClientConn.t(),
  text :: String.t(),
  options :: map() | nil
) ::
  {:ok, term()} | {:error, term()}

Sends a text message.

status(conn, options)

@callback status(conn :: WebsockexNova.ClientConn.t(), options :: map() | nil) ::
  {:ok, atom()} | {:error, term()}

Gets the current connection status.

subscribe(conn, channel, options)

@callback subscribe(
  conn :: WebsockexNova.ClientConn.t(),
  channel :: String.t(),
  options :: map() | nil
) ::
  {:ok, term()} | {:error, term()}

Subscribes to a channel or topic.

unregister_callback(conn, pid)

@callback unregister_callback(conn :: WebsockexNova.ClientConn.t(), pid :: pid()) ::
  {:ok, WebsockexNova.ClientConn.t()}

Unregisters a process from receiving notifications.

unsubscribe(conn, channel, options)

@callback unsubscribe(
  conn :: WebsockexNova.ClientConn.t(),
  channel :: String.t(),
  options :: map() | nil
) ::
  {:ok, term()} | {:error, term()}

Unsubscribes from a channel or topic.