ChannelServer
ChannelServer module: runtime helpers for Phoenix-style channel processes.
This module provides the building blocks for implementing channel processes using March's actor system. The actual actor declarations must be provided by the application (actors cannot be spawned from closures in March).
A channel process is an actor that:
- Receives a ChannelJoin message with topic/params.
- Calls the handler's join/3 callback.
- On success, subscribes to PubSub for the topic.
- Loops receiving ChannelMailbox messages and dispatching to callbacks.
- On ChannelLeave, unsubscribes from PubSub and calls leave/3.
Example actor using these helpers:
actor MyChannelProcess do state = ChannelServer.init_state()
on ChannelIn(ref, event, payload) -> ChannelServer.handle_in_msg(state, ref, event, payload)
on ChannelBroadcast(topic, event, payload) -> ChannelServer.handle_broadcast(state, topic, event, payload)
on ChannelLeave(reason) -> ChannelServer.handle_leave(state, reason) end
PubSub shard actors are similarly declared by the framework user:
actor PubSubShard do state = PubSub.new_state()
on PubSubSubscribe(topic, pid) -> PubSub.subscribe_state(state, topic, pid)
on PubSubUnsubscribe(topic, pid) -> PubSub.unsubscribe_state(state, topic, pid)
on PubSubBroadcast(topic, excl, event, payload) -> PubSub.broadcast_to(PubSub.get_subscribers(state, topic), excl, event, payload) state end
Types
Functions
Construct a ChannelConfig.
Extract the socket from a config.
Extract the pubsub shard pid from a config.
Perform the join handshake. Calls join_fn, subscribes to PubSub on success. Returns JoinOk(state) or JoinErr(reason). Must be called from within an actor handler (requires actor runtime).
Perform the leave teardown. Unsubscribes from PubSub, calls leave_fn. Must be called from within an actor handler (requires actor runtime).
Apply a HandleResult: if Reply, send the reply to the transport; return the new state. For Stop, call do_leave. Returns the new channel state. Must be called from within an actor handler (requires actor runtime).
Handle a ChannelBroadcast message received by a channel process. Forwards the event+payload to the transport pid. Must be called from within an actor handler (requires actor runtime).
Handle a ChannelBroadcastFrom message. Forwards event+payload to the transport unless this process is excluded. Must be called from within an actor handler (requires actor runtime).
Process a PubSubSubscribe message inside a shard actor. Returns the new PubSubState.
Process a PubSubUnsubscribe message inside a shard actor. Returns the new PubSubState.
Process a PubSubBroadcast message inside a shard actor. Fans out to all subscribers, optionally excluding a pid. Returns the PubSubState unchanged.
Select the shard pid for topic from a list of shard pids. Uses PubSub.topic_shard for consistent routing.