enchufeweb v0.2.1 Enchufeweb behaviour View Source

Enchufeweb is a websocket client library written in Elixir and based on the Erlang library websocket_client.

Link to this section Summary

Functions

It will start (linked) the websocket client

It will send the given message using the given websocket(pid)

Callbacks

Callback which will be called when the connection has been made

Callback which will be called when the connection is closed

Callback which will be called when a message is received

Link to this section Types

Link to this type conn_mode() View Source
conn_mode() :: :disconnected | :once | :reconnect
Link to this type frame() View Source
frame() :: :close | :ping | :pong | binary
Link to this type state() View Source
state() :: any
Link to this type websocket_req() View Source
websocket_req() :: map

Link to this section Functions

Link to this function start_link(list) View Source
start_link(url: binary, ws_opts: map) ::
  {:ok, pid} |
  {:error, term}

It will start (linked) the websocket client.

The argument will be a keyword list:

  • url: String. For instance ws://host:port/endpoint
  • ws_opts: It has to be a map which has to contain the connection mode (%{conn_mode: conn_mode})

    • :disconnected : It will begin with the client in a disconnected mode
    • :once : It only tries one connection
    • :reconnect : It will try to reconnect until it get it
Link to this function ws_send(ws, message) View Source
ws_send(pid, frame) :: :ok

It will send the given message using the given websocket(pid)

Link to this section Callbacks

Link to this callback handle_connection(websocket_req, state) View Source
handle_connection(websocket_req, state) ::
  {:ok, state} |
  {:ok, number, state} |
  {:reply, frame, state} |
  {:close, binary, state}

Callback which will be called when the connection has been made.

Input:

  • Websocket request information
  • Current state

Output:

  • {:ok, state}
  • {:ok, keepalive, state} : keepalive will be the interval in ms for sending pings to the server.
  • {:reply, reply, state} : It will directly send reply to the server.
  • {:close, reason, state} : It will close the connection due to reason.
Link to this callback handle_disconnection(websocket_req, state) View Source
handle_disconnection(websocket_req, state) ::
  {:ok, state} |
  {:reconnect, state} |
  {:reconnect, integer, state} |
  {:close, binary, state}

Callback which will be called when the connection is closed

Input:

  • Websocket request information
  • Current state

Output:

  • {:ok, state} : The process continues although the connection is closed.
  • {:reconnect, state} : It tries to reconnect.
  • {:reconnect, delay, state} : It tries to reconnect after delay ms.
  • {:close, reason, state} : It terminates the process.
Link to this callback handle_message(frame, state) View Source
handle_message(frame, state) ::
  {:ok, state} |
  {:reply, frame, state} |
  {:close, binary, state}

Callback which will be called when a message is received.

The argument has to be a binary message or one of these atoms: :close, :ping or :pong

Output:

  • {:ok, state} : nothing occurs
  • {:reply, reply, state} : It will send reply to the server.
  • {:close, reason, state} : It will close the connection due to reason.