ZenWebsocket.MessageHandler (ZenWebsocket v0.4.2)

Copy Markdown View Source

Message handling utilities for WebSocket connections.

  • Parse incoming WebSocket frames and Gun messages
  • Route messages to user-provided handler functions
  • Handle control frames (ping/pong) automatically
  • Process WebSocket upgrade responses

API Functions

FunctionArityDescriptionParam Kinds
create_handler1Create a callback function for handling specific message types.opts: value
default_handler1Default message handler that accepts and discards messages.message: value
handle_control_frame3Handle WebSocket control frames automatically.decoded_frame: value, conn_pid: value, stream_ref: value
decode_and_handle_control1Decode a WebSocket frame and handle control frames automatically.frame_tuple: value
handle_message2Handle incoming Gun messages and WebSocket frames.message: value, handler_fun: value

Summary

Functions

Create a callback function for handling specific message types.

Decode a WebSocket frame and handle control frames automatically. Returns decoded data frames without invoking any handler callback.

Default message handler that simply logs messages.

Handle WebSocket control frames automatically. Returns :handled for control frames, :not_control for data frames.

Handle incoming Gun messages and WebSocket frames. Routes messages to appropriate handler function.

Functions

create_handler(opts \\ [])

@spec create_handler(keyword()) :: (term() -> any())

Create a callback function for handling specific message types.

decode_and_handle_control(arg)

@spec decode_and_handle_control(tuple()) ::
  {:ok, {:data, {atom(), binary()}}}
  | {:ok, :control_frame_handled}
  | {:error, {:protocol_error, term()}}

Decode a WebSocket frame and handle control frames automatically. Returns decoded data frames without invoking any handler callback.

Used by the Client GenServer which has its own routing layer (route_data_frame) for JSON parsing, subscription routing, and heartbeat handling.

default_handler(message)

@spec default_handler(term()) :: :ok

Default message handler that simply logs messages.

handle_control_frame(arg1, conn_pid, stream_ref)

@spec handle_control_frame(term(), pid(), reference()) :: :handled | :not_control

Handle WebSocket control frames automatically. Returns :handled for control frames, :not_control for data frames.

handle_message(message, handler_fun \\ &default_handler/1)

@spec handle_message(tuple(), (term() -> any())) ::
  {:ok, term()} | {:error, {:protocol_error, term()}}

Handle incoming Gun messages and WebSocket frames. Routes messages to appropriate handler function.