Fluxon Docs
Batteries

ws — websocket (realtime)

Persistent two-way connections, per-connection session state, and rooms for broadcast.

For real-time applications (chat, live updates). Where http is request-response, ws is a persistent two-way connection.

use ws

ws.on :connect \conn ->         # a new connection. conn.id — a stable unique id
  ws.data.set conn :user nil    # ws.data — session state for THIS connection

ws.on :message \conn msg ->     # msg — the incoming text (if JSON, json.dec it)
  m = json.dec msg
  ws.send conn (json.enc {ok:true})    # reply to THIS connection

ws.on :disconnect \conn ->
  ws.room.leave conn "ch:5"

ws.serve 9000
  • ws.on :event handler — events: :connect, :message, :disconnect. The :message handler is \conn msg -> (msg — the incoming text), the others are \conn ->.
  • ws.send conn text — sends to THIS connection (text; if you need JSON, json.enc).
  • ws.data.set conn :key value / ws.data.get conn :key — session state for THIS connection (Fluxon keeps it until the connection drops, and clears it on disconnect).
  • ws.serve port — starts the server (blocking).

Rooms — for broadcast

Sending to a group at once. Fluxon manages rooms itself — you do not maintain a manual "who is in which room" map:

ws.room.join conn "ch:5"                          # add the connection to a room
ws.room.leave conn "ch:5"                         # remove it from the room
ws.room.send "ch:5" (json.enc {t:"msg" body:b})   # send to EVERYONE in the room
ws.room.members "ch:5"                            # the room members (for presence)

http.serve and ws.serve work together (on different ports). Room membership and presence are managed inside ws.room — no manual shared-state map is needed.

On this page