Skip to main content

ClientId

string: an opaque, random identifier (UUID) of a client session. A client connected over both TCP and UDP has the same ClientId on both servers.

Methods

Register NetworkServerLibrary from @nanoforge-dev/network/server. Systems reach it through ctx.network, which exposes the methods below; the library instance has them too. They take an optional { channel } to pick one of the four channels: Channel.ReliableOrdered, Channel.ReliableUnordered, Channel.UnreliableOrdered or Channel.UnreliableUnordered. Reliable channels need LISTENING_TCP_PORT and unreliable ones LISTENING_UDP_PORT. Using a channel whose port is not configured throws, except on a peer-to-peer host, where every channel works. The server guide has examples.

sendToAll(data, options?)

Send a payload to every client reachable on the channel. Parameter data: NetworkPayload, bytes, a string or any JSON value. Option channel: Channel to send on. Channel.ReliableOrdered by default. Throws a TypeError when data cannot be encoded as JSON (undefined, a function). Returns: void

sendToClient(clientId, data, options?)

Send a payload to one client. Logs an error when the client is not reachable on the channel. Throws a TypeError when data cannot be encoded as JSON (undefined, a function). Parameter clientId: ClientId client identifier. Parameter data: NetworkPayload, bytes, a string or any JSON value. Option channel: Channel to send on. Channel.ReliableOrdered by default. Returns: void

getReceivedPackets(options?)

Return the packets each client sent since the last call. Call it once per frame: packets left unread for a whole tick are dropped. Typed packets never show up here. Option channel: Channel to read from. Without it, the packets of every configured channel are merged per client, reliable channels first. Returns: Map<ClientId, NetworkData[]>, see NetworkData

getConnectedClients(options?)

Return a snapshot array of the clients reachable on the channel: clients with an open WebSocket for reliable channels, clients with the channel’s data channel for unreliable ones. Option channel: Channel to check. Without it, clients reachable on any configured channel. Returns: ClientId[]

getClientInfo(clientId)

Return what is known about a client session. See ClientInfo. Parameter clientId: ClientId Returns: ClientInfo | undefined (undefined once the client is gone)

packets (ctx.network.packets)

Typed packets, declared with definePacket (see the network-client API). Packets of a type are routed to its listeners and to read, never to getReceivedPackets.

on(packet, listener)

Call listener(data, { clientId, channel, ctx }) for every packet of this type, each tick before the ECS systems (not while paused). Packets are dispatched in the order they arrived, reliable channels first. A listener that throws, or returns a rejected promise, is logged and does not stop the others. Returns: () => void, a function that removes the listener.

read(packet)

Return the packets of this type received this tick, as { data, clientId, channel }[]. Meant for ECS systems, which run after the packets are dispatched. Returns: { data: T; clientId: ClientId; channel: Channel }[]

sendToAll(packet, data?, options?)

Send a packet to every client reachable on the channel. data must match the definition’s payload type and is encoded as JSON. Leave it out for a packet without payload: sendToAll(Start). Option channel: Channel to send on. The definition’s channel by default. Throws a TypeError when data cannot be encoded as JSON.

sendToClient(clientId, packet, data?, options?)

Send a packet to one client, like sendToAll.

NetworkPayload and NetworkData

Payloads are encoded and read back the same way on both sides: see NetworkPayload and NetworkData in the client API. Passing a received NetworkData to sendToAll or sendToClient relays a client’s packet as it is.

clients (ctx.network.clients)

Client sessions shared by the TCP and UDP servers. Also available as NetworkServerLibrary.clients.

get(clientId)

Returns: ClientInfo | undefined

list()

Return every connected client session. Returns: ClientInfo[]

onConnect(listener)

Call listener(info) when a new client session starts, i.e. when its first transport is up. With TCP and UDP, it fires on TCP open, before UDP is linked: sendToClient(info.id) on an unreliable channel may still report an unknown client at that point. With UDP only, it fires when the signaling socket opens, before the data channels exist, so it may also report an unknown client. Returns: () => void that removes the listener.

onDisconnect(listener)

Call listener(info) when a client session ends, i.e. when its last transport closes. Returns: () => void that removes the listener.

ClientInfo

ConnectionInfo

Cookie and Authorization headers are never recorded.