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

Redis broadcast

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

The in-memory adapter reaches the connections of one process. ulo-ws-redis carries a broadcast to the members held by every process on the same Redis. Handlers do not change between the two. Only the module's adapter does.

Choosing the adapter

examples/24-ws-rooms picks the adapter in its root module, from REDIS_URL:

use ulo::{Module, ModuleDef, ModuleIdentity};
use ulo_ws::WsModule;
use ulo_ws_redis::Redis;

impl Module for Root {
    fn identity(&self) -> ModuleIdentity {
        ModuleIdentity::of_type::<Self>()
    }

    fn register(&self, m: &mut ModuleDef<'_>) {
        match &self.redis {
            Some(url) => m.import(WsModule::for_root().broadcast(Redis::url(url))),
            None => m.import(WsModule::for_root()),
        };
        m.controller::<Lobby>();
        m.controller::<Notifier>();
    }
}

With a fixed Redis, the module is one line (from the ulo-ws-redis crate documentation):

#[module(imports = [ulo_ws::WsModule::for_root().broadcast(ulo_ws_redis::Redis::url("redis://cache:6379"))])]
pub struct AppModule;

The crate ships the adapter value and no module. A second module binding the broadcast adapter beside WsModule would be a DuplicateBinding at wire().

redis://host:6379 is plain and rediss://host:6380 selects TLS. The URL is parsed and the connection made lazily, at the first publish or subscribe.

Run two processes on one Redis and a shout on either reaches both:

REDIS_URL=redis://127.0.0.1:6379 PORT=3000 cargo run -p ex-24-ws-rooms
REDIS_URL=redis://127.0.0.1:6379 PORT=3001 cargo run -p ex-24-ws-rooms

Two registrations of the adapter

The module's settings read from the URL are the connection target, username, database, protocol and, on rediss, certificate verification. Two imports whose settings are equal are one registration, and two that differ are refused at wire() naming the setting, as are different connect_within values.

  • The target is redis://, rediss:// or redis+unix://, then the host and port with 6379 filled in. The host's ASCII letters are lowercased, so redis://Cache and redis://cache are equal. A trailing dot is kept, and redis://cache. and redis://cache are refused naming url, since a resolver can send them to different servers.
  • database is the URL's path, 0 unset: redis://a:6379 and redis://a:6379/0 are equal.
  • username is the ACL user, default unset. The client authenticates only with a password, so redis://alice@cache is default and equals redis://cache, while redis://alice:key@cache and redis://bob:key@cache are refused.
  • protocol is resp2 unset, or resp3 from ?protocol=resp3.
  • #insecure on a rediss URL turns certificate verification off. A registration differing only in it is refused.
  • A password is never compared or printed: two URLs differing only in it are equal, and the first registration's is used.

How it works

Each room and each client has a Pub/Sub channel:

ChannelCarries
ulo:ws:room:<room>a room's broadcasts
ulo:ws:node:<node>broadcasts to one client, addressed to the process its id names
ulo:ws:allbroadcasts to everyone

A process subscribes to its own node channel and ulo:ws:all, and pattern-subscribes to ulo:ws:room:*. Membership stays local to each process: the adapter learns of no membership change, so each process filters room messages against its own members on delivery.

The frame travels between processes as JSON, {"event","data"}, whatever the gateways' codecs. Each process re-encodes it per gateway on delivery, with that gateway's event field and codec. On a MessagePack gateway a byte string therefore arrives as an array of numbers.

Guarantees

  • Delivery is best effort, ordered per sender within one process.
  • A broadcast published before another process's subscription is in place is lost. listen() returns once Redis has confirmed this process's own subscription, so a broadcast published after listen() reaches this process's members as well as the others'.
  • A lost subscription reconnects after one second.

Startup

A subscription Redis does not confirm within Redis::connect_within(d) (30 seconds unset) fails listen() as StartupError::Bind, with the last connection error as the cause. A zero bound is refused in prepare.

The adapter's connections and its subscription task run on a tokio runtime that the adapter holds. Redis::url(..) captures the tokio runtime current where it is built; with_handle(handle) names another. An adapter built outside a tokio runtime and given none fails connect, naming .with_handle(..). The app itself may run on another runtime.

Messages from a newer or older build

A message the subscription cannot read is dropped with a warn and the subscription reads on. Messages in another wire version arise during a rolling deploy. The first of each version or unknown audience kind is logged at warn, later ones at debug.

Testing against Redis

examples/24-ws-rooms/tests/across_processes.rs runs two processes of the binary on one Redis in a container:

cargo test -p ex-24-ws-rooms --features integration --test across_processes

It needs a running Docker daemon.

Rooms and broadcast

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

Separate port

Serving a gateway on its own port with ulo-ws-hyper, and choosing between that and the HTTP server's port.

On this page

Choosing the adapterTwo registrations of the adapterHow it worksGuaranteesStartupMessages from a newer or older buildTesting against Redis