Skip to main content

Module ws

Module ws 

Source
Expand description

WebSocket server + chat-broadcast registry.

net.serve_ws(port, on_message) blocks on a TCP listener, upgrades each incoming connection to WebSocket, and runs a per-connection worker thread that polls both inbound (calls Lex’s on_message) and outbound (drains broadcasts from a channel into the socket).

chat.broadcast(room, body) looks up every connection in room and pushes body onto its outbound channel. chat.send(conn_id, body) is the same but to a single connection.

The registry is an Arc<Mutex<…>> because Lex’s immutability means shared mutable state has to live in the host runtime. Lex code stays pure: it receives an event, returns Nil, and any side effects go through chat.* which is gated by the policy.

Structs§

ChatRegistry
Global chat registry. One per net.serve_ws invocation.

Functions§

chat_broadcast
chat.broadcast(room, body) — looked up at runtime by the effect handler; called from inside the Lex VM.
chat_send
serve_ws
Bind a WebSocket server. Blocks; returns Unit on shutdown (the process is normally killed before that).
serve_ws_fn
Closure-based WebSocket server. Accepts a Value::Closure as the handler.