WebsockexNova.Gun.ConnectionState (WebsockexNova v0.1.1)
View SourceState structure for ConnectionWrapper.
This module defines the state structure used by the ConnectionWrapper, providing better type safety and organization of state data.
IMPORTANT: State Layering
This struct should ONLY contain transport-layer, process-local, or Gun-specific data. It should NOT store canonical application-level state like:
- Authentication
- Subscriptions
- User credentials
- Application handler state
The canonical application state lives in WebsockexNova.ClientConn
.
This struct may reference a ClientConn struct for operations, but should
never duplicate the canonical state.
Reconnection Policy
- The connection state no longer tracks reconnection attempts or delays.
- All reconnection policy and tracking is handled by the error handler.
Summary
Functions
Adds an active stream to the state with the given status and data.
Clears all active streams from the state.
Gets the callback PID from the state.
Creates a new connection state.
Prepares the state for process termination by clearing references and resources.
Records an error in the state.
Removes a stream from the active streams map.
Removes multiple streams from the active streams map.
Sets up the auth handler module and stores it in the handlers map.
Sets up the connection handler module and stores it in the handlers map.
Sets up the error handler module and stores it in the handlers map.
Setup the logging handler module.
Sets up the message handler module and stores it in the handlers map.
Sets up the subscription handler module and stores it in the handlers map.
Updates the active streams map.
Updates the callback PID in the state.
Updates the connection handler state within the handlers map.
Updates the Gun connection monitor reference in the state.
Updates the Gun connection PID in the state.
Updates a handler module reference.
Updates multiple handlers at once in the handlers map.
Updates the connection status in the state.
Updates or adds a stream in the active streams map.
Types
@type status() ::
:initialized
| :connecting
| :connected
| :websocket_connected
| :disconnected
| :reconnecting
| :error
| :closed
Connection status
@type t() :: %WebsockexNova.Gun.ConnectionState{ active_streams: %{required(reference()) => map()}, callback_pid: pid() | nil, client_conns: %{required(reference()) => struct()}, gun_monitor_ref: reference() | nil, gun_pid: pid() | nil, handlers: %{ optional(:connection_handler) => module(), optional(:message_handler) => module(), optional(:error_handler) => module(), optional(:logging_handler) => module(), optional(:subscription_handler) => module(), optional(:auth_handler) => module(), optional(:rate_limit_handler) => module(), optional(:metrics_collector) => module() }, host: String.t(), last_error: term() | nil, options: map(), path: String.t(), port: non_neg_integer(), status: status(), transport: atom(), ws_opts: map() }
Connection state structure
Functions
Adds an active stream to the state with the given status and data.
Parameters
state
- Current connection statestream_ref
- Stream reference to addstream_data
- Data for the stream, can be either an atom status or a map with data
Returns
Updated connection state struct
Clears all active streams from the state.
Parameters
state
- Current connection state
Returns
Updated connection state struct with empty active_streams
Gets the callback PID from the state.
Parameters
state
- Current connection state
Returns
The callback PID, or nil if none exists
@spec new(String.t(), non_neg_integer(), map()) :: t()
Creates a new connection state.
Parameters
host
- The hostname to connect toport
- The port to connect tooptions
- Connection options (only transport configuration is stored)
Returns
A new connection state struct
Prepares the state for process termination by clearing references and resources.
Parameters
state
- Current connection state
Returns
Updated connection state ready for termination
Records an error in the state.
Parameters
state
- Current connection stateerror
- Error to record
Returns
Updated connection state struct
Removes a stream from the active streams map.
Parameters
state
- Current connection statestream_ref
- Stream reference to remove
Returns
Updated connection state struct
Removes multiple streams from the active streams map.
Parameters
state
- Current connection statestream_refs
- List of stream references to remove
Returns
Updated connection state struct
Sets up the auth handler module and stores it in the handlers map.
Parameters
state
- Current connection stateauth_handler
- Auth handler moduleoptions
- Handler options (not stored in ConnectionState)
Returns
Updated connection state struct
Sets up the connection handler module and stores it in the handlers map.
Parameters
state
- Current connection stateconnection_handler
- Connection handler moduleoptions
- Handler options (not stored in ConnectionState)
Returns
Updated connection state struct
Sets up the error handler module and stores it in the handlers map.
Parameters
state
- Current connection stateerror_handler
- Error handler moduleoptions
- Handler options (not stored in ConnectionState)
Returns
Updated connection state struct
Setup the logging handler module.
Parameters
state
- Current connection statelogging_handler
- Logging handler module_options
- Logging handler options (not stored in ConnectionState)
Returns
Updated connection state struct
Sets up the message handler module and stores it in the handlers map.
Parameters
state
- Current connection statemessage_handler
- Message handler moduleoptions
- Handler options (not stored in ConnectionState)
Returns
Updated connection state struct
Sets up the subscription handler module and stores it in the handlers map.
Parameters
state
- Current connection statesubscription_handler
- Subscription handler moduleoptions
- Handler options (not stored in ConnectionState)
Returns
Updated connection state struct
Updates the active streams map.
Parameters
state
- Current connection stateactive_streams
- Map of stream references to stream data
Returns
Updated connection state struct
Updates the callback PID in the state.
Parameters
state
- Current connection statepid
- New callback PID
Returns
Updated connection state struct
Updates the connection handler state within the handlers map.
Parameters
state
- Current connection statehandler_state
- New handler state to store
Returns
Updated connection state struct
Updates the Gun connection monitor reference in the state.
Parameters
state
- Current connection statemonitor_ref
- Monitor reference for the Gun process
Returns
Updated connection state struct
Updates the Gun connection PID in the state.
Parameters
state
- Current connection stategun_pid
- The Gun connection process PID
Returns
Updated connection state struct
Updates a handler module reference.
Parameters
state
- Current connection statehandler_key
- Key for the handler (:connection_handler, :message_handler, etc.)module
- Handler module
Returns
Updated connection state struct
Updates multiple handlers at once in the handlers map.
Parameters
state
- Current connection statehandlers
- Map of handler keys to handler modules or state
Returns
Updated connection state struct
Updates the connection status in the state.
Parameters
state
- Current connection statestatus
- New status to set
Returns
Updated connection state struct
Updates or adds a stream in the active streams map.
Parameters
state
- Current connection statestream_ref
- Stream referencestatus
- Stream status
Returns
Updated connection state struct