Connection & Reconnection
The client connection lifecycle - connect, auto-reconnect, token refresh, errors, and kicks.
Connecting
connect() resolves once the server has fully established the session - after your authentication hook ran and admission placed the session in its initial instances. It rejects if the server refuses the connection - with a PlayMeshError carrying a machine-readable code - and throws if the client is already connected or connecting.disconnect() tears the connection down and clears client.session. A later connect() starts fresh.ℹ
The client announces its wire-protocol version during the handshake. PlayMesh 0.7 speaks wire protocol v3: a mismatch (e.g. a 0.6.x client against a 0.7.0 server) is rejected on both sides with code
VERSION_MISMATCH - upgrade @playmesh/server and @playmesh/client together.Error codes
Every error carries a
code: ServerError.code in onError, and error.code on rejected connect(), join() and requestTo() promises. Servers may also send their own application codes (thrown as PlayMeshError).Automatic reconnection
Reconnection is handled by the underlying Socket.IO client and is on by default. Tune it through the
socket option:On every reconnect the server re-authenticates and re-admits the session, and the client re-seeds its instance list, presence, and synced-state replicas from the new join snapshots. Pending
requestTo() promises reject on disconnect - they are never silently replayed.Token refresh
Pass
auth as a function and it is re-evaluated on every connection attempt - so an expired token is refreshed before each reconnect instead of failing the handshake:Lifecycle handlers
ℹ
error.scope tells you what failed: connection (setup/auth), event (an emit was rejected server-side), join (a client.join() was vetoed - error.instance names the instance), or chat (a message was blocked).Serialization
JSON is the default wire encoding. Set
serialization: 'msgpack'to use binary msgpack frames (usually noticeably smaller for game traffic). It must match the server's serialization option - a mismatched client cannot connect.Reconnection with signing
With
signing enabled, every connection gets a fresh session nonce from the server. After a reconnect the client waits for the new nonce before sending signed events; events queued for the old connection are dropped and reported via onError rather than replayed. See Auth & Admission for the full signing model.