Slack.WebSocketClient behaviour (SlackKit v0.25.0-alpha.0)

View Source

A small WebSocket client process built on top of Mint.WebSocket.

The public surface intentionally mirrors the Erlang :websocket_client library so that Slack.Bot (and any test stubs that follow the same contract) can use it as a drop-in replacement:

Slack.WebSocketClient.start_link(url, callback_module, state, opts)
Slack.WebSocketClient.cast(pid, {:text, "..."})

Callback modules implement the Slack.WebSocketClient behaviour, which re-uses the function names from :websocket_client:

  • init/1
  • onconnect/2
  • ondisconnect/2
  • websocket_handle/3
  • websocket_info/3
  • websocket_terminate/3

Summary

Functions

Sends a frame on the WebSocket. Mirrors :websocket_client.cast/2.

Returns a specification to start this module under a supervisor.

Starts the WebSocket client and connects to url.

Types

frame()

@type frame() ::
  {:text, binary()}
  | {:binary, binary()}
  | :ping
  | {:ping, binary()}
  | :pong
  | {:pong, binary()}
  | :close
  | {:close, non_neg_integer(), binary()}

handler_response()

@type handler_response() ::
  {:ok, any()} | {:reply, frame(), any()} | {:close, any(), any()}

Callbacks

init(any)

@callback init(any()) :: {:ok, any()} | {:once, any()} | {:reconnect, any()}

onconnect(any, any)

@callback onconnect(any(), any()) :: {:ok, any()}

ondisconnect(any, any)

@callback ondisconnect(any(), any()) ::
  {:ok, any()} | {:reconnect, any()} | {:close, any(), any()}

websocket_handle(frame, any, any)

@callback websocket_handle(frame(), any(), any()) :: handler_response()

websocket_info(any, any, any)

@callback websocket_info(any(), any(), any()) :: handler_response()

websocket_terminate(any, any, any)

@callback websocket_terminate(any(), any(), any()) :: :ok

Functions

cast(pid, frame)

Sends a frame on the WebSocket. Mirrors :websocket_client.cast/2.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

start_link(url, module, state, opts \\ [])

Starts the WebSocket client and connects to url.

url may be a string or charlist (the latter is accepted for compatibility with the older :websocket_client callers).

Options

  • :keepalive - how often (in ms) to send a keepalive ping. Defaults to :infinity (no keepalive).