PlayMesh/Docs
HomeGitHubnpm

Server API

Complete reference for @playmesh/server — PlayMesh, Domain, Instance, and Session.

PlayMesh

The root class. Import from @playmesh/server.

Constructor

import { PlayMesh } from '@playmesh/server';
const mesh = new PlayMesh(options?: PlayMeshOptions);
Method / PropertyDescriptionReturns
portPort to listen on. Defaults to 3000. Pass 0 for ephemeral port.number
serverAttach to a pre-existing HTTP server.http.Server
redisRedis connection options or URL string. Enables distributed mode.RedisOptions | string
socketOptions forwarded to the Socket.IO server.Partial<ServerOptions>

Topology

Method / PropertyDescriptionReturns
createDomain(id)Create and register a new Domain.Domain
domain(id)Get an existing Domain by id. Throws if not found.Domain
hasDomain(id)Check whether a Domain with this id exists.boolean
domainsAll registered Domains.Domain[]
sessionsAll active Sessions on this node.Session[]
resolveInstance(ref)Resolve "domainId/instanceId" or a bare instance id.Instance

Hooks

Method / PropertyDescriptionReturns
bootstrap(hook)Async setup before the server accepts connections.this
onAuthenticate(hook)Register the authentication hook.this
onAdmission(hook)Decide which instances a session joins.this
onSessionCreate(hook)Called once per session, after authentication.this
onConnect(hook)Called when a session connects.this
onDisconnect(hook)Called when a session disconnects.this
onStarted(hook)Called after the server is listening.this
onShutdown(hook)Called at the start of graceful shutdown.this

Messaging & extensions

Method / PropertyDescriptionReturns
broadcast(event, payload?)Send an event to every connected session across all nodes.void
use(middleware)Register a middleware function.this
use(plugin)Install a plugin with an install() method.this

Infrastructure & lifecycle

Method / PropertyDescriptionReturns
ioThe underlying Socket.IO server. Available after start().SocketIOServer
redisThe shared Redis client (when Redis is configured).Redis
queuesThe QueueManager (requires Redis).QueueManager
start()Start the server.Promise<{ port: number }>
shutdown()Gracefully shut down.Promise<void>
metrics()Current runtime metrics.Metrics
const mesh = new PlayMesh({ port: 4000, redis: { host: 'localhost', port: 6379 } });

mesh.bootstrap(async ({ mesh }) => { /* create domains and instances */ });
mesh.onAuthenticate(async request => ({ userId: request.auth.userId as string }));
mesh.onStarted(() => console.log('Server ready'));

const { port } = await mesh.start();

process.on('SIGTERM', () => mesh.shutdown());

Domain

Retrieved via mesh.createDomain(id) or mesh.domain(id).
Method / PropertyDescriptionReturns
idThe domain identifier.string
instancesAll Instances in this domain.Instance[]
createInstance(id)Create a new Instance.Instance
instance(id)Get an Instance by id.Instance
hasInstance(id)Check whether an Instance exists.boolean
destroyInstance(id)Remove sessions, clear state, delete the instance.Promise<void>
broadcast(event, payload?)Send to every session in any instance of this domain.void
onInstanceCreated(hook)Called when a new instance is created.this
onInstanceDestroyed(hook)Called when an instance is destroyed.this

Instance

An isolated multiplayer environment. Sessions join to receive broadcasts and have events routed.
Method / PropertyDescriptionReturns
idThe instance identifier.string
domainThe owning Domain.Domain
path"domainId/instanceId" — globally unique reference.string
sessionsSessions on this node that are members.Session[]
stateSynchronized runtime key-value state.ScopedState
on(event, handler)Register an event handler.this
off(event, handler)Remove an event handler.this
broadcast(event, payload?)Send to all member sessions across all nodes.void
memberCount()Presence-backed total member count across all nodes.Promise<number>
onJoin(hook)Called when a session joins.this
onLeave(hook)Called when a session leaves.this
instance.sessions is local-node only. Use instance.memberCount() for the cluster total.

Session

Represents a single connected client.
Method / PropertyDescriptionReturns
idSocket ID (unique per connection).string
userIdAssigned by your authenticate hook.string
dataApplication data from the auth result.Record<string, unknown>
instancesInstances this session is currently a member of.Instance[]
isIn(instance)Check membership by Instance or id string.boolean
join(instance)Join an instance. No-op if already a member.Promise<void>
leave(instance)Leave an instance. No-op if not a member.Promise<void>
send(event, payload?)Send an event to this client only.void
disconnect()Forcibly disconnect this client.void

ScopedState

Method / PropertyDescriptionReturns
get(key)Get a value. Returns undefined if not set.Promise<unknown>
set(key, value)Set a value (JSON-serialized).Promise<void>
delete(key)Delete a key.Promise<void>
keys()List all keys in this instance scope.Promise<string[]>
clear()Delete all keys in this instance scope.Promise<void>

Metrics

const m = mesh.metrics();
// {
//   sessions: number,   — active sessions on this node
//   domains: number,
//   instances: number,
//   uptimeMs: number,
// }