ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

Protocols

WebSocketGatewaysMessage handlers with DIRooms and broadcastRedis broadcastSeparate portLimits and shutdown

Integrations

Operating

Reference

WebSocket

Rooms and broadcast

Joining rooms, broadcasting to rooms, clients and everyone, and pushing to WebSocket clients from an HTTP route.

A room is a named set of connections. examples/24-ws-rooms joins connections to rooms from a message and broadcasts to them from both a WebSocket handler and an HTTP route.

Joining and leaving

A connection joins a room through its Connection handle:

use ulo_ws::{Payload, WsCx};

#[ulo_ws::message("join")]
async fn join(&self, room: Payload<String>, cx: WsCx) {
    cx.conn().join(room.0).await;
}

conn.leave(room) leaves. OnConnect can join a room at connect with cx.conn().join("lobby").await. A connection leaves every room when it ends, after on_disconnect has run, so the hook still sees the connection's rooms through conn.rooms().

Broadcasting

Rooms is one binding, from WsModule. Inject it with Dep<Rooms> anywhere:

rooms.to_room(room).emit("shouted", &text).await
CallAudience
rooms.to_all()every connection of every gateway
rooms.to_room(name)every member of the room
rooms.to_client(conn_id)one connection
.except([ids]) on the resultthe same, minus those connection ids

emit(event, &data) answers Result<(), BroadcastError>. Members receive the frame as a message with that event name: {"event":"shouted","data":"<text>"}. A broadcast frame carries no id.

To limit a broadcast to one gateway, address it first. rooms.gateway::<Lobby>() or rooms.namespace("lobby") answers a RoomsIn with the same to_all, to_room, to_client and except methods.

rooms.gateway::<ChatGateway>().to_room("lobby").except([me]).emit("chat.message", &m).await?;

Pushing from HTTP

Rooms is an ordinary injectable, so any controller on any transport can broadcast through the adapter the gateways use. POST /notify/{room} in example 24:

use ulo::{Dep, controller, handlers};
use ulo_http::{Json, Path};
use ulo_ws::{BroadcastError, Rooms};

#[controller]
pub struct Notifier {
    rooms: Dep<Rooms>,
}

#[handlers]
impl Notifier {
    /// Delivers `text` to the members of `room` as a `notice` event.
    #[ulo_http::post("/notify/{room}")]
    async fn notify(&self, Path(room): Path<String>, Json(notice): Json<Notice>) -> Result<&'static str, BroadcastError> {
        self.rooms.to_room(room).emit("notice", &notice.text).await?;
        Ok("sent")
    }
}

The gateway in that example shares the HTTP server's port (no port = own), so one port serves both:

curl -X POST -H 'content-type: application/json' -d '{"text":"hello"}' http://127.0.0.1:3000/notify/lobby

The adapter

Delivery goes through a BroadcastAdapter. WsModule::for_root() binds the in-memory adapter, which reaches this process's connections only. It is a 1,024-deep broadcast channel: the sender never waits, and a subscriber that falls behind loses the oldest broadcasts, logged at warn.

To reach connections held by other processes, give the module the Redis adapter: Redis broadcast.

An adapter is configuration on the module and comes with no module of its own. Two WsModule imports with different adapter settings fail at wire() as WiringError::DifferingSettings.

Where a client is addressed

A connection id (ConnId) names its process, so rooms.to_client(id) reaches a client held by another process when the Redis adapter is in use. Membership in rooms is local to each process: a process delivers a room broadcast to the members it holds.

Message handlers with DI

What a WebSocket handler can take as parameters and return, and how dependencies reach a gateway.

Redis broadcast

Carrying room and client broadcasts between processes with ulo-ws-redis.

On this page

Joining and leavingBroadcastingPushing from HTTPThe adapterWhere a client is addressed