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.
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:
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:
Or as a container. The image is one static binary of about 7 MB:
Then point the client at 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
| Variable | Default | What it sets |
|---|---|---|
SPKY_DB_URL | required | The SurrealDB endpoint the relay asks who a token belongs to (http(s):// or ws(s)://). |
SPKY_DB_NS | required | Namespace. A token of another namespace is refused. |
SPKY_DB_NAME | required | Database. A token of another database is refused. |
SPKY_RELAY_LISTEN_ADDR | 0.0.0.0:8668 | Address to listen on. |
SPKY_RELAY_MAX_ROOM_PEERS | 16 | Browsers in one room. One more is refused with room_full. |
SPKY_RELAY_MAX_FRAME_BYTES | 65536 | Largest frame. A larger one closes the connection. |
SPKY_RELAY_RATE_BURST | 400 | Frames a connection may send at once. |
SPKY_RELAY_RATE_PER_SEC | 200 | Frames per second it may keep sending. Frames above that are dropped. |
SPKY_RELAY_QUEUE_FRAMES | 256 | Frames waiting for one member. A member that falls further behind is disconnected. |
SPKY_RELAY_SESSION_SECS | 3600 | Longest one connection lasts. Then the client is told to sign in again and reconnects. |
SPKY_RELAY_ALLOWED_ORIGINS | any | Comma 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_LOG | sp00ky_relay=info,warn | Log filter. sp00ky_relay=debug logs every join and leave. A token is never logged. |
GET /health answers 200 ok.
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.idand 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.idis 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.
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.
hellomust be the first frame, within 10 seconds.peeris the browser’s own id, 1 to 64 characters ofA-Z a-z 0-9 _ -.welcomenames the user and the browsers already in the room. Everybody else getsjoin.msggoes to the members named into, or to all others without it, and arrives withfromset by the relay.dis any JSON value and is not looked at.leavetells the others a browser is gone.pingis answered withpong. 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:
| Code | Meaning |
|---|---|
bad_request | A malformed frame, a bad peer, an unknown version. |
unauthorized | The token is invalid or belongs to no user. |
unavailable | The relay could not reach the database. |
room_full | The room has SPKY_RELAY_MAX_ROOM_PEERS members. |
replaced | Another connection joined with the same peer and took its place. The others see that peer leave and join. |
rate_limited | The connection kept sending above its rate. |
reauth | The 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.