howdy/websocket/channel
Named groups of sockets to broadcast to.
A socket joins a topic, and anything running anywhere in the program can send a frame to every socket on that topic: an HTTP handler, a service, a background job, or another socket.
import howdy/websocket
import howdy/websocket/channel
websocket.new(fn(socket) {
channel.join(socket, "room:" <> room)
Nil
})
|> websocket.on_text(fn(_socket, state, text) {
channel.broadcast_text("room:" <> room, text)
websocket.continue(state)
})
Topics live in an Erlang pg scope that howdy starts on first use.
Membership is tied to the socket process, so a socket that closes or
crashes is removed from every topic automatically. If the app runs on
several connected nodes, a broadcast reaches sockets on all of them.
Values
pub fn broadcast(topic: String, frame: websocket.Frame) -> Nil
Send a frame to every socket on a topic. The frames go out from each socket’s own process, so this returns at once and never blocks the caller on a slow client.
pub fn broadcast_json(topic: String, body: json.Json) -> Nil
Send JSON as a text frame to every socket on a topic.
pub fn broadcast_text(topic: String, text: String) -> Nil
Send a text frame to every socket on a topic.
pub fn join(socket: websocket.Socket(msg), topic: String) -> Nil
Subscribe a socket to a topic. Joining a topic twice has no extra effect.
pub fn leave(socket: websocket.Socket(msg), topic: String) -> Nil
Unsubscribe a socket from a topic. Leaving a topic the socket is not on does nothing.