Remarks
Reads network configuration from the environment viaClientConfigNetwork
and opens the connections during __init, so the channels are open before
the game runs: TCP (WebSocket) carries the reliable channels and UDP (WebRTC
data channels) the unreliable ones. TCP connects first; once welcomed, UDP
presents the session token so both transports share one client id.
Configuration (via environment variables):
SERVER_ADDRESS— hostname or IP of the server (required).SERVER_TCP_PORT— WebSocket port for TCP (optional).SERVER_UDP_PORT— signaling port for UDP/WebRTC (optional).WSS— set to"true"to usewss:///https://(default:false).ICE_SERVERS— STUN/TURN servers for the UDP transport, comma-separated or a JSON array (default:[]).NETWORK_MODE—p2p(set bynffromnetwork: "p2p"): without?room=in the page URL, this player hosts the game, running the server app in this tab; with it, the player joins that room’s host over WebRTC. The signaling server is reached atSERVER_ADDRESSandSERVER_TCP_PORT.
Example
Extends
Library
Implements
Constructors
Constructor
new NetworkClientLibrary(): NetworkClientLibrary
Defined in: modules/network/src/client/client.network.library.ts:80
Returns
NetworkClientLibrary
Overrides
Library.constructor
Properties
key
Defined in: modules/network/src/client/client.network.library.ts:53 This library’s identity: its slot onreadonlykey:"network"
Context, its registry key, and
the identifier used to reference it in ordering relationships.
Overrides
Library.key
Accessors
clientId
Get Signature
get clientId():Defined in: modules/network/src/client/client.network.library.ts:64 Client id assigned by the server,string|undefined
undefined until transport is welcomed.
Returns
string | undefined
inviteLink
Get Signature
get inviteLink():Defined in: modules/network/src/client/client.network.library.ts:74 Seestring|undefined
NetworkClientContextApi.inviteLink.
Returns
string | undefined
packets
Get Signature
get packets(): ClientPacketsApi
Defined in: modules/network/src/client/client.network.library.ts:86
Typed packets: per-type listeners, read and send.
Returns
ClientPacketsApi
relationships
Get Signature
get relationships(): LibraryRelationships
Defined in: packages/common/dist/index.d.ts:512
Internal
Returns
LibraryRelationships
Inherited from
Library.relationships
roomCode
Get Signature
get roomCode():Defined in: modules/network/src/client/client.network.library.ts:69 Seestring|undefined
NetworkClientContextApi.roomCode.
Returns
string | undefined
Methods
__clear()
__clear(Defined in: packages/common/dist/index.d.ts:525 Called once after the tick loop stops._ctx):Promise<void>
Parameters
_ctx
Context
Returns
Promise<void>
Inherited from
Library.__clear
__editorInit()
__editorInit(Defined in: modules/network/src/client/client.network.library.ts:172 Traces packets for the editor (ctx):Promise<void>
network-trace, network-stats) while
its welcome asks for it.
Parameters
ctx
EditorInitContext
Returns
Promise<void>
Implementation of
EditorAwareLibrary.__editorInit
__events()
__events(Defined in: modules/network/src/client/client.network.library.ts:181 Sends the totals of the packets traced for the editor (also while paused)._ctx):Promise<void>
Parameters
_ctx
Context
Returns
Promise<void>
Overrides
Library.__events
__init()
__init(Defined in: modules/network/src/client/client.network.library.ts:90 Called once, in dependency order, before the tick loop starts.ctx):Promise<void>
Parameters
ctx
InitContext
Returns
Promise<void>
Overrides
Library.__init
__run()
__run(Defined in: modules/network/src/client/client.network.library.ts:186 Calls the listeners of the typed packets received since the last tick.ctx):Promise<void>
Parameters
ctx
Context
Returns
Promise<void>
Overrides
Library.__run
expose()
expose(): NetworkClientContextApi
Defined in: modules/network/src/client/client.network.library.ts:190
Returns this library’s public contribution to Context[this.key].
Returns
NetworkClientContextApi
Remarks
Return a plain object exposing only what game code should be able to reach — neverthis — so internal state and lifecycle hooks never leak
onto Context. Libraries with nothing to expose (headless libraries)
can leave this unimplemented; they simply won’t appear on Context.
Overrides
Library.expose
getReceivedPackets()
getReceivedPackets(Defined in: modules/network/src/client/client.network.library.ts:154 Return the packets received since the last call.options?):NetworkData[]
Parameters
options?
ChannelOptions
channel to read from, every channel by default.
Returns
NetworkData[]
Array of packets, each read with bytes(), text(), json()…
Remarks
Call this method once per frame: packets left unread for a whole tick are dropped, and typed packets never show up here. Without achannel, the
packets of every configured channel are returned, reliable channels first.
Throws
When the channel’s port was not configured.isConnected()
isConnected(Defined in: modules/network/src/client/client.network.library.ts:164 Returnoptions?):boolean
true when packets can be sent and received.
Parameters
options?
ChannelOptions
channel to check, every configured channel by default.
Returns
boolean
Throws
When the channel’s port was not configured.sendData()
sendData(Defined in: modules/network/src/client/client.network.library.ts:138 Send a payload to the server.data,options?):void
Parameters
data
NetworkPayload
Bytes, a string, or any value JSON.stringify can encode.
options?
ChannelOptions
channel to send on, Channel.ReliableOrdered by default.
Returns
void
Throws
When the channel’s port was not configuredthrowNotInitializedError()
Defined in: packages/common/dist/index.d.ts:536protectedthrowNotInitializedError():never
Returns
never
Inherited from
Library.throwNotInitializedError