Tunnel
The server-to-server channel — envelope, handlers, validation and fan-out rules
Two NOX nodes talk over one endpoint:
POST https://<peer>/api/tunnel
{ "id": "<handler>", "data": { … } }id selects the handler registered with @OnTunnel(id) on the receiving node; data is its
body, validated against a DTO before the handler runs. The response is the handler's return value.
The tunnel is a shortcut, never a single point of failure: when it fails (a peer that only speaks ActivityPub, a node momentarily down), the caller falls back to the ActivityPub inbox or simply logs — a write never fails because a notification could not travel.
Identity
There is no origin field. The peer is the address the authenticated request came from
(ctx.server.address), proven by the tunnel handshake. A declared address could only ever repeat
it, so a node cannot announce a change for somebody else's account.
Handlers
id | Direction | Body | Response |
|---|---|---|---|
user.update | profile change | { id, action, entity? } | { notified } |
user.relation | follow edges between users | { id, action, targets: number[] } | { applied, failed } |
asset.update | asset change | { type, id, action, entity? } | { delivered } |
asset.relation | follow edges on an asset | { type, id, action, targets: number[] } | { applied } |
notification | user notification | { to, type, key, from, data, expires_at } | — |
user.update
{
"id": "user.update",
"data": {
"id": 42,
"action": "updated",
"entity": { "id": 42, "username": "alice", "relations": null, "…": "ApiUserDto" }
}
}entityis the public sanitised view (ApiUserDto), built without a viewer. It is optional: a sender that only knows the account changed omits it and the receiver emits the bare signal.- The receiver resolves its own audience (
COUNT+ keyset pages on its relation table) and emitsuser.changedto those local users. Nothing about the audience crossed the wire. notifiedis the number of local users the frame was addressed to.
user.relation
{
"id": "user.relation",
"data": { "id": 42, "action": "unfollow", "targets": [3, 9, 11] }
}idis the account on the sending node,targetsare numeric ids on this node.actionis one offollow,unfollow,unfollow_reverse,follow_accept,follow_refuse.- Batched: a purge or a follow-policy change moves dozens of edges in one round trip instead of one per edge.
- Best effort per target: an unknown account increments
failedand does not drop the others. The call only fails when nothing could be applied (failed > 0 && applied === 0), which is what a singlefollowneeds to report a refusal.
asset.update
{
"id": "asset.update",
"data": { "type": "world", "id": 12, "action": "updated", "entity": { "…": "vue publique" } }
}actioniscreated,updatedorremoved;entityis absent for a removal.- The receiver resolves its own followers from the mirror written by
asset.relation(u:<id>@:: → w:<n>@<origin>) and emits<kind>.updated(or<kind>.removed) to them, one audience page at a time. deliveredis the number of local users the frame was addressed to.
asset.relation
{
"id": "asset.relation",
"data": { "type": "world", "id": 12, "action": "follow", "targets": [3, 9] }
}actionisfolloworunfollow.- This is the message that carries ids: the peer owns the edge, so it tells this node which of
its users moved, and this node mirrors it locally with the origin in the ref
(
w:12@<origin>). That mirror is what makesasset.updateresolvable without a recipient list. - Sent by the hosting node when a peer's
Follow/Undoactivity is accepted, best effort.
notification
{
"id": "notification",
"data": {
"to": "42", "type": "location:request", "key": "loc:42:7",
"from": "7", "data": {}, "expires_at": 1762000000000
}
}expires_at: null is a transient notification: delivered to live sockets, never persisted.
Otherwise the receiving node stores it so the recipient can list it later.
Validation
Every body is validated like an HTTP request: plainToInstance on the handler's DTO, then
class-validator with whitelist and forbidNonWhitelisted.
- an unknown property is a
400, not a silent drop; - a nested entity (
user.update) is validated as a real DTO (ApiUserDto), field by field; - a missing handler is a
404.
Delivery rules
| Rule | Why |
|---|---|
| No audience in an update | an entity frame is identical for every recipient; ids travel in a relation message |
Audience pages (AUDIENCE_PAGE = 1000) | a million followers cost one page of heap, not a million refs |
Concurrent peers (PEER_BATCH = 8) | a slow or dead peer never holds the others behind it |
| Queued, not inline | asset fan-out runs in the job queue (one job per peer), never in the caller's request |
| ActivityPub fallback | a server whose followers are named accounts gets the Create/Update/Delete activity instead |
Calling a peer
await this.tunnel.send(server, 'user.update', {
id: user.id,
action: 'updated',
entity: profile,
});send discovers the peer (ExternalServersService.discover), POSTs { id, data } to its
/tunnel, and resolves with the handler's response. It throws an ApiException when the peer is
unreachable or its handler fails — callers that must not fail on it (fan-outs) catch and log.
Implementing a peer
- Register a handler with
@OnTunnel(id, Dto)— ortunnel.register(id, fn, Dto)for a class instantiated several times (asset kinds). - Treat
entityas a cache payload: it is the public view, sorelationsand viewer-scoped fields are absent by design. - Resolve your own audience from your own relation rows; never expect a list of recipients in an update.
- Keep the mirror accurate: when one of your users starts or stops following a remote asset, send
asset.relation— it is what lets the peer's updates reach that user later.