Bingex.Socket behaviour (Bingex v0.1.7)

Provides an abstraction over WebSocket connections for BingX.

This module builds on WebSockex, handling connection management, message passing, and event dispatching while hiding implementation details. It allows consumers to define their own event handlers through callback functions.

Key features:

  • Automatic handling of WebSocket connections, including reconnections.
  • Message passing using call/3, cast/2, and send/2.
  • Support for compressed binary frames (e.g., handling gzip-compressed data).
  • Flexible event handling via callback functions.

This module is designed to simplify WebSocket integration while allowing customization through user-defined modules.

Summary

Functions

Sends a synchronous message.

Sends an asynchronous message.

Sends a message to the process to the specified PID.

Starts a WebSocket process.

Starts a WebSocket process linked to the current process.

Returns the pid of a Socket process, nil otherwise.

Types

event()

@type event() :: term()

message()

@type message() :: binary()

state()

@type state() :: term()

Callbacks

handle_call(message, from, state)

(optional)
@callback handle_call(message(), from :: pid(), state()) ::
  {:reply, message :: term(), state()} | {:ok, state()} | {:disconnect, state()}

handle_cast(message, state)

(optional)
@callback handle_cast(message(), state()) ::
  {:send, message(), state()} | {:ok, state()} | {:disconnect, state()}

handle_connect(state)

(optional)
@callback handle_connect(state()) :: {:ok, state()}

handle_disconnect(details, state)

(optional)
@callback handle_disconnect(
  details :: WebSockex.connection_status_map(),
  state()
) :: {:stop, state()} | {:reconnect, state()}

handle_event(event, state)

@callback handle_event(event(), state()) :: {:ok, state()} | {:disconnect, state()}

handle_info(message, state)

(optional)
@callback handle_info(
  message(),
  state()
) :: {:send, message(), state()} | {:ok, state()} | {:disconnect, state()}

Functions

call(dest, message, timeout \\ 5000)

Sends a synchronous message.

cast(dest, message)

Sends an asynchronous message.

send(dest, message)

Sends a message to the process to the specified PID.

start(url, module, state, options \\ [])

Starts a WebSocket process.

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

Starts a WebSocket process linked to the current process.

whereis(dest)

@spec whereis(dest :: atom() | pid()) :: pid() | nil

Returns the pid of a Socket process, nil otherwise.