Skip to main content
Register NetworkClientLibrary from @nanoforge-dev/network/client. 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 SERVER_TCP_PORT and unreliable ones SERVER_UDP_PORT. Using a channel whose port is not configured throws. The client connects during __init, so its channels are open once the game runs. TCP connects first, then UDP with the TCP session token, so both share one client id. __init rejects when a transport fails, or when its channels are not open within 10 seconds. The client guide has examples.

sendData(data, options?)

Send a payload to the server. 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

getReceivedPackets(options?)

Return the packets received 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 returned, reliable channels first. Returns: NetworkData[]

packets

Typed packets, declared with definePacket (see below). Packets of a type are routed to its listeners and to read, never to getReceivedPackets.

packets.on(packet, listener)

Call listener(data, { 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.

packets.read(packet)

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

packets.send(packet, data?, options?)

Send a packet to the server. data must match the definition’s payload type and is encoded as JSON. Leave it out for a packet without payload, declared without a type: send(Play). Option channel: Channel to send on. The definition’s channel by default. Throws a TypeError when data cannot be encoded as JSON. Returns: void

definePacket(name, options?)

Declare a packet type, in a file both apps import. Also exported by @nanoforge-dev/network itself, for that shared file. Parameter name: string, 1 to 255 UTF-8 bytes, unique in the game. It is written in front of every packet of this type. Option channel: Channel used when send gets no channel. Channel.ReliableOrdered by default. Option validate: (data: unknown) => data is T, run on every received payload before listeners and read. Packets that fail it are dropped with a warning. Throws when the name is empty or too long, and later, in packets.on or read, when another definition of the same name has another channel or validate. Returns: PacketDefinition<T>. Without a type, T is void: the packet carries no payload.

isConnected(options?)

Return true when packets can be sent and received. Option channel: Channel to check. Without it, every configured channel must be connected. Returns: boolean

clientId

Client id assigned by the server, shared by TCP and UDP. undefined until the server has welcomed the client. It is only kept in memory: a page reload gets a new id. Type: string | undefined In peer-to-peer mode, the code of the room the client hosts or joined, and a link other players open to join it. See the peer-to-peer page for when they are set.

NetworkPayload

What sendData accepts. Its payload carries no format tag: the receiver reads a packet the way it was sent.

NetworkData

One received packet. Its methods are synchronous and return a new value on every call.