March Docs

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

typeSocketSocket = Socket(#
typeHandleResultHandleResult(a) = Reply(String, String, a) | NoReply(a) | Stop(String, a)#
typeChannelMailboxChannelMailbox =#
typePubSubMsgPubSubMsg(a) =#
typeBroadcastMsgBroadcastMsg = BroadcastMsg(String, String)#
typeLeaveReasonLeaveReason = NormalLeave | Disconnect | Kicked(String)#
typeChannelRouteChannelRoute = ChannelRoute(String, String)#
typeChannelMsgChannelMsg = ChannelMsg(String, String, String, String, String)#

Functions

fnsocket_topicsocket_topic(sock)#

Return the topic of a socket.

fnsocket_channel_pidsocket_channel_pid(sock)#

Return the channel pid of a socket.

fnsocket_transport_pidsocket_transport_pid(sock)#

Return the transport pid of a socket.

fnsocket_assignssocket_assigns(sock)#

Return the assigns list of a socket.

fnsocket_join_refsocket_join_ref(sock)#

Return the join_ref of a socket.

fnget_assignget_assign(sock, key)#

Look up a value in socket assigns by key. Returns Some(val) or None.

fnassignassign(sock, key, value)#

Assign a key-value pair to the socket. Returns an updated socket.

fnnew_socketnew_socket(topic, channel_pid, transport_pid, join_ref)#

Create a new socket with the given topic, channel pid, transport pid.

fnreplyreply(status, payload, st)#

Construct a Reply result: reply to the client with status and JSON payload.

fnnoreplynoreply(st)#

Construct a NoReply result: no response to the client.

fnstopstop(reason, st)#

Construct a Stop result: close the channel.

fnpushpush(sock, event, payload)#

Push a message directly to this client only. Sends an actor message to the transport pid: Push(event, payload). Requires actor runtime.

fnbroadcastbroadcast(sock, event, payload)#

Broadcast event+payload to ALL subscribers of the socket's topic (including self). Routes through the PubSub shard. Requires actor runtime.

fnbroadcast_frombroadcast_from(sock, event, payload)#

Broadcast event+payload to all subscribers of the socket's topic EXCEPT the sender (this channel process). Requires actor runtime.

fnchannel_routechannel_route(pattern, handler_name)#

Construct a ChannelRoute. Pattern may end in '*' for prefix matching.

fnroute_patternroute_pattern(r)#

Return the pattern part of a ChannelRoute.

fnroute_handlerroute_handler(r)#

Return the handler name part of a ChannelRoute.

fnmake_msgmake_msg(join_ref, ref, topic, event, payload)#

Construct a ChannelMsg.

fnmsg_join_refmsg_join_ref(m)#

Return the join_ref of a ChannelMsg.

fnmsg_refmsg_ref(m)#

Return the ref of a ChannelMsg.

fnmsg_topicmsg_topic(m)#

Return the topic of a ChannelMsg.

fnmsg_eventmsg_event(m)#

Return the event of a ChannelMsg.

fnmsg_payloadmsg_payload(m)#

Return the payload string of a ChannelMsg.

fnserializeserialize(m)#

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).

fnparseparse(json_str)#

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.

fnreply_msgreply_msg(join_ref, ref, topic, status, response_payload)#

Construct a phx_reply message (sent by server in response to a client ref).

fnerror_msgerror_msg(join_ref, ref, topic, reason)#

Construct a phx_error message (sent on join rejection or handler Stop).

fnclose_msgclose_msg(join_ref, topic)#

Construct a phx_close message (sent when the channel is closed).