Github|...

Room relay

sp00ky-relay is a small WebSocket server that puts the devices of one user into a shared room and forwards their optimistic updates to each other, in memory, without reading them.

Experimental

The relay belongs to sharing with your own devices, which is experimental. The protocol may change between releases.

sp00ky-relay is the server a user’s devices meet on. A client (a browser, a Flutter app, a Rust app) signs in to it with its session token, lands in the room of that user, and everything it sends there is passed on to the other devices in the room. That is how a change made on the phone shows on the laptop before the database has seen it.

It is deliberately small:

  • Memory only. No disk, no database of its own. A restart forgets every room and the clients reconnect.
  • It does not read what it forwards. A message is copied to the other members byte for byte.
  • Nothing depends on it. A message that gets lost is one the normal sync delivers a moment later, so the relay never retries or stores anything.

Run it

With the CLI it is one line in sp00ky.yml:

sp00ky.yml
relay: true

spky dev then starts it on port 8668, and spky deploy (or a git-linked push) puts it at wss://{slug}-spky-relay.<domain>/room. Either way your frontend gets the address as VITE_SPKY_RELAY_URL. The host is the platform’s, not one of your apps’, so a service of your own named relay is left alone. See relay in sp00ky.yml.

On your own servers it is one Rust binary (apps/relay), configured through the environment:

SPKY_DB_URL=http://localhost:8666 \
SPKY_DB_NS=main \
SPKY_DB_NAME=main \
cargo run --release -p sp00ky-relay
# listening on 0.0.0.0:8668

Or as a container. The image is one static binary of about 7 MB:

# from the repository root
docker build -f apps/relay/Dockerfile -t sp00ky-relay .
docker run --rm -p 8668:8668 \
  -e SPKY_DB_URL=ws://surrealdb:8000 -e SPKY_DB_NS=main -e SPKY_DB_NAME=main \
  sp00ky-relay

Then point the client at it:

const db = new SyncedDb({
  // ...
  channels: {
    // set by spky dev and spky deploy with relay: true; else your own address
    relay: import.meta.env.VITE_SPKY_RELAY_URL,
    share: { tables: { game: true } },
  },
});
Put TLS in front of it

The relay speaks plain WebSocket. Run it behind the reverse proxy that already terminates TLS for your other services and give the clients a wss:// address: the first frame carries a session token.

The image mono424/spooky-relay is published with every release since 1.0.0-beta.9, under the same tags as the SSP and the scheduler. To run a build of your own, name it in relay.image (spky dev runs a local image; a deploy pulls it, so push it to a registry first). Cloudflare projects cannot run it yet.

A room lives in the memory of one process, so all devices of a user have to reach the same instance. One instance is enough for a long way: it holds a socket per browser and copies small frames.

Configuration

VariableDefaultWhat it sets
SPKY_DB_URLrequiredThe SurrealDB endpoint the relay asks who a token belongs to (http(s):// or ws(s)://).
SPKY_DB_NSrequiredNamespace. A token of another namespace is refused.
SPKY_DB_NAMErequiredDatabase. A token of another database is refused.
SPKY_RELAY_LISTEN_ADDR0.0.0.0:8668Address to listen on.
SPKY_RELAY_MAX_ROOM_PEERS16Browsers in one room. One more is refused with room_full.
SPKY_RELAY_MAX_FRAME_BYTES65536Largest frame. A larger one closes the connection.
SPKY_RELAY_RATE_BURST400Frames a connection may send at once.
SPKY_RELAY_RATE_PER_SEC200Frames per second it may keep sending. Frames above that are dropped.
SPKY_RELAY_QUEUE_FRAMES256Frames waiting for one member. A member that falls further behind is disconnected.
SPKY_RELAY_SESSION_SECS3600Longest one connection lasts. Then the client is told to sign in again and reconnects.
SPKY_RELAY_ALLOWED_ORIGINSanyComma separated list of origins that may connect. A request without an Origin header (a native app) always passes: the token is what protects a room.
RUST_LOGsp00ky_relay=info,warnLog filter. sp00ky_relay=debug logs every join and leave. A token is never logged.

GET /health answers 200 ok.

Limit connections in the proxy

Every new connection costs one query to your database before the relay knows who is calling, and the relay does not cap connections per address. Do that in the reverse proxy in front of it.

Who gets into which room

  • Identity comes from SurrealDB. On connect the relay asks your database, with the client’s session token and as that user, for $auth.id and for the namespace and database of the session. An invalid or expired token is refused, and so is a valid token of another database on the same SurrealDB instance. The relay holds no credentials of its own.
  • On SurrealDB 3.x the user must be able to read its own record, since $auth.id is read with the user’s permissions there.
  • The room is the user. A connection is put into the room named by that id and nowhere else, so a client cannot ask for somebody else’s room. There are no rooms between users yet.
  • Your permissions are untouched. The relay only carries what one device of a user tells another device of the same user. Every write still goes to SurrealDB through the normal sync, where your table permissions decide, and a device drops what the server does not confirm.
What the relay can see

Messages cross the relay in memory in plain form, like requests cross the SSP. A direct link between two browsers is encrypted end to end between them, a message through the room is not.

Protocol

One WebSocket at /room, JSON text frames. This is what the web client speaks; you only need it to write another client.

// client -> relay
{ "t": "hello", "v": 1, "token": "<session token>", "peer": "k3J9x" }
{ "t": "msg", "to": ["p2"], "d": { "any": "json" } }
{ "t": "msg", "d": { "any": "json" } }
{ "t": "ping" }

// relay -> client
{ "t": "welcome", "user": "user:abc", "peer": "k3J9x", "peers": ["p2", "p3"] }
{ "t": "join", "peer": "p4" }
{ "t": "leave", "peer": "p2" }
{ "t": "msg", "from": "p2", "d": { "any": "json" } }
{ "t": "pong" }
{ "t": "error", "code": "unauthorized", "message": "..." }
  1. hello must be the first frame, within 10 seconds. peer is the browser’s own id, 1 to 64 characters of A-Z a-z 0-9 _ -.
  2. welcome names the user and the browsers already in the room. Everybody else gets join.
  3. msg goes to the members named in to, or to all others without it, and arrives with from set by the relay. d is any JSON value and is not looked at.
  4. leave tells the others a browser is gone.
  5. ping is answered with pong. The relay also sends WebSocket pings and drops a connection it has heard nothing from for a minute.

to: [] means nobody, a peer named twice gets the frame once, and the sender never gets its own frame back. There is no acknowledgement and no replay.

A refusal is {"t":"error","code":"..."} followed by a close:

CodeMeaning
bad_requestA malformed frame, a bad peer, an unknown version.
unauthorizedThe token is invalid or belongs to no user.
unavailableThe relay could not reach the database.
room_fullThe room has SPKY_RELAY_MAX_ROOM_PEERS members.
replacedAnother connection joined with the same peer and took its place. The others see that peer leave and join.
rate_limitedThe connection kept sending above its rate.
reauthThe session reached SPKY_RELAY_SESSION_SECS. Connect again with the current token.

An idle connection and one that sent an oversized frame are closed without an error frame. A member whose queue is full is dropped without a close (a browser reports code 1006). The full contract is in apps/relay/README.md.