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
| Function | Arity | Description | Param Kinds |
|---|---|---|---|
create_handler | 1 | Create a callback function for handling specific message types. | opts: value |
default_handler | 1 | Default message handler that accepts and discards messages. | message: value |
handle_control_frame | 3 | Handle WebSocket control frames automatically. | decoded_frame: value, conn_pid: value, stream_ref: value |
decode_and_handle_control | 1 | Decode a WebSocket frame and handle control frames automatically. | frame_tuple: value |
handle_message | 2 | Handle 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 a callback function for handling specific message types.
@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.
@spec default_handler(term()) :: :ok
Default message handler that simply logs messages.
Handle WebSocket control frames automatically. Returns :handled for control frames, :not_control for data frames.
@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.