LiveState.Channel behaviour (live_state v0.6.0)
To build a LiveState application, you'll first want to add a channel that implements this behaviour.
Link to this section Summary
Callbacks
Called from join to authorize the connection. Return {:ok, socket}
to authorize or
{:error, reason}
to deny. Default implementation returns {:ok, socket}
Receives an event an payload from the client and current state. Returns the new state along with (optionally)
a single or list of LiveState.Event
to dispatch to client
Receives pubsub message and current state. Returns new state
Returns the initial application state. Called just after connection
The key on assigns to hold application state. Defaults to :state
.
The key on assigns to hold application state version. Defaults to :version
.
Link to this section Callbacks
authorize(channel, payload, socket)
@callback authorize(channel :: binary(), payload :: term(), socket :: Socket.t()) :: {:ok, socket :: Socket.t()} | {:error, binary()}
Called from join to authorize the connection. Return {:ok, socket}
to authorize or
{:error, reason}
to deny. Default implementation returns {:ok, socket}
handle_event(event_name, payload, state)
@callback handle_event(event_name :: binary(), payload :: term(), state :: term()) :: {:reply, reply :: %LiveState.Event{detail: term(), name: term()} | [%LiveState.Event{detail: term(), name: term()}], new_state :: any()} | {:noreply, new_state :: map()}
Receives an event an payload from the client and current state. Returns the new state along with (optionally)
a single or list of LiveState.Event
to dispatch to client
handle_event(event_name, payload, state, socket)
@callback handle_event( event_name :: binary(), payload :: term(), state :: term(), socket :: Socket.t() ) :: {:reply, reply :: %LiveState.Event{detail: term(), name: term()} | [%LiveState.Event{detail: term(), name: term()}], new_state :: map(), Socket.t()} | {:noreply, new_state :: map(), Socket.t()}
handle_message(message, state)
@callback handle_message(message :: term(), state :: term()) :: {:reply, reply :: %LiveState.Event{detail: term(), name: term()} | [%LiveState.Event{detail: term(), name: term()}], new_state :: any()} | {:noreply, new_state :: term()}
Receives pubsub message and current state. Returns new state
init(channel, payload, socket)
@callback init(channel :: binary(), payload :: term(), socket :: Socket.t()) :: {:ok, state :: map() | Socket.t()} | {:error, reason :: any()}
Returns the initial application state. Called just after connection
state_key()
@callback state_key() :: atom()
The key on assigns to hold application state. Defaults to :state
.
state_version_key()
@callback state_version_key() :: atom()
The key on assigns to hold application state version. Defaults to :version
.