Channel
Channel module: Phoenix-style real-time channels for March.
Channels provide multiplexed bidirectional messaging over a single transport (WebSocket, long-poll, …). Each channel join creates a green thread that calls user-defined callbacks:
join/3 — called on join attempt; returns Ok(state) or Err(reason) handle_in/4 — called when the client sends an event handle_info/3 — called when the channel process receives an actor message leave/3 — called on leave / disconnect
Types defined here: Socket — per-connection state (topic, assigns, pids) HandleResult — return type for channel callbacks ChannelMsg — JSON wire message (topic/event/payload/ref/join_ref) ChannelRoute — pattern → handler association used by the router LeaveReason — reason a channel is leaving
Helper functions (usable inside channel handlers): reply/3 — reply to client + update state noreply/1 — no reply, update state stop/2 — stop channel with reason assign/3 — add/update a key in socket.assigns push/3 — push a message to this specific client broadcast/3 — broadcast to all topic subscribers broadcast_from/3 — broadcast to all subscribers except self
Wire format (JSON over WebSocket): { "topic": "room:lobby", "event": "new_msg", "payload": {...}, "ref": "1", "join_ref": "0" }
Topic pattern matching (ChannelRoute): "room:*" matches "room:lobby", "room:42", etc. "notify:" matches exactly "notify:"
See also: channel_server.march (runtime loop), presence.march.
Types
Functions
Return the topic of a socket.
Return the channel pid of a socket.
Return the transport pid of a socket.
Return the assigns list of a socket.
Return the join_ref of a socket.
Look up a value in socket assigns by key. Returns Some(val) or None.
Assign a key-value pair to the socket. Returns an updated socket.
Create a new socket with the given topic, channel pid, transport pid.
Construct a Reply result: reply to the client with status and JSON payload.
Construct a NoReply result: no response to the client.
Construct a Stop result: close the channel.
Push a message directly to this client only. Sends an actor message to the transport pid: Push(event, payload). Requires actor runtime.
Broadcast event+payload to ALL subscribers of the socket's topic (including self). Routes through the PubSub shard. Requires actor runtime.
Broadcast event+payload to all subscribers of the socket's topic EXCEPT the sender (this channel process). Requires actor runtime.
Construct a ChannelRoute. Pattern may end in '*' for prefix matching.
Return the pattern part of a ChannelRoute.
Return the handler name part of a ChannelRoute.
Construct a ChannelMsg.
Return the join_ref of a ChannelMsg.
Return the ref of a ChannelMsg.
Return the topic of a ChannelMsg.
Return the event of a ChannelMsg.
Return the payload string of a ChannelMsg.
Serialize a ChannelMsg to a JSON string conforming to the Phoenix channel wire format: {"join_ref":"0","ref":"1","topic":"room:lobby","event":"new_msg","payload":{}} The payload field is embedded verbatim (it should already be a JSON value).
Parse a JSON channel message string into a ChannelMsg. Returns Some(msg) on success, None on parse failure. Performs minimal parsing: extracts the five fields using string search.
Construct a phx_reply message (sent by server in response to a client ref).
Construct a phx_error message (sent on join rejection or handler Stop).
Construct a phx_close message (sent when the channel is closed).