Dev's NoxDev's Nox
Protocol

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

idDirectionBodyResponse
user.updateprofile change{ id, action, entity? }{ notified }
user.relationfollow edges between users{ id, action, targets: number[] }{ applied, failed }
asset.updateasset change{ type, id, action, entity? }{ delivered }
asset.relationfollow edges on an asset{ type, id, action, targets: number[] }{ applied }
notificationuser 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" }
  }
}
  • entity is 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 emits user.changed to those local users. Nothing about the audience crossed the wire.
  • notified is the number of local users the frame was addressed to.

user.relation

{
  "id": "user.relation",
  "data": { "id": 42, "action": "unfollow", "targets": [3, 9, 11] }
}
  • id is the account on the sending node, targets are numeric ids on this node.
  • action is one of follow, 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 failed and does not drop the others. The call only fails when nothing could be applied (failed > 0 && applied === 0), which is what a single follow needs to report a refusal.

asset.update

{
  "id": "asset.update",
  "data": { "type": "world", "id": 12, "action": "updated", "entity": { "…": "vue publique" } }
}
  • action is created, updated or removed; entity is 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.
  • delivered is 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] }
}
  • action is follow or unfollow.
  • 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 makes asset.update resolvable without a recipient list.
  • Sent by the hosting node when a peer's Follow / Undo activity 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

RuleWhy
No audience in an updatean 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 inlineasset fan-out runs in the job queue (one job per peer), never in the caller's request
ActivityPub fallbacka 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

  1. Register a handler with @OnTunnel(id, Dto) — or tunnel.register(id, fn, Dto) for a class instantiated several times (asset kinds).
  2. Treat entity as a cache payload: it is the public view, so relations and viewer-scoped fields are absent by design.
  3. Resolve your own audience from your own relation rows; never expect a list of recipients in an update.
  4. 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.

On this page