Skip to main content
NetworkServerLibrary, from @nanoforge-dev/network/server, accepts client connections in the game server. See Network to register it and set its environment variables.

Overview

The server listens on configured ports and accepts client connections. A single server process can accept many clients, and typical responsibilities in a game are:
  • Accept reliable control messages from clients on Channel.ReliableOrdered and Channel.ReliableUnordered, carried by a TCP WebSocket.
  • Optionally establish RTCPeerConnections via WebSocket signaling for Channel.UnreliableOrdered and Channel.UnreliableUnordered, carried by WebRTC data channels, for low-latency state updates.
See Packet Channels for what each channel guarantees.

Client identification

Every client gets a ClientId: a random string (UUID) shared by the TCP and UDP transports.
  1. The first transport a client opens starts a session. The server answers with a welcome message carrying the id and a secret session token (a text frame on TCP, where game data is always binary, and a JSON message on the UDP signaling socket).
  2. The client presents that token (?token=...) when it opens its other transport, which joins the same session. An unknown token, or a second connection on a transport the session already has, is rejected with 401.
  3. The session ends when its last transport closes.
A page reload opens a new session, with a new ClientId.
onConnect fires as soon as the first transport is up, so when both transports are used, UDP may not be linked yet.

Example

Typed packets

The server uses the same packet types as the client, from the shared protocol file (see Typed packets). Its listeners and read also get the sender.
Payloads come from the clients, so check the ones the game cannot trust with the validate option of definePacket.

Notes