Server Engine Hooks
Server engine hooks allow you to listen to server-level events and customize server behavior. These hooks are defined in theengine property of your server module.
Usage
Available Hooks
onStart
Description: Called when the server starts Parameters:server: RpgServerEngine- The server engine instance
onStep
Description: Called for map rooms, once after each queued map tick has been processed. It is not emitted for lobby rooms. A queued tick is one pass through the server scheduler; depending on the accumulated delta, it can execute zero, one, or several fixed simulation steps. Empty maps stop their automatic loop, so observability does not keep an idle room active. Parameters:server: RpgServerEngine- The server engine instancemetrics: RpgServerStepMetrics- Metrics for the processed map tick:tick- Current cumulative fixed simulation tick after processingdurationMs- Wall-clock processing duration in milliseconds, excluding the hook itselfscheduledDeltaMs- Delta in milliseconds passed to the queued tickqueuedDeltaMs- Delta in milliseconds that arrived during processing and remains queuedfixedSteps- Number of fixed simulation steps executed (which can be zero or greater)pendingInputs- Total unprocessed player inputs after the tick
server remain compatible. Async handlers are awaited
in the tick queue. If a handler throws or rejects, RPGJS logs the error for the
automatic loop and retries queued simulation work on a later tick. Avoid network
I/O here because it directly increases the server backlog.
Example:
auth
Description: Flexible authentication function for player connections. This function is called during each RPGJS room connection and should handle credential verification before the player is allowed to join the room. Parameters:server: RpgServerEngine- The server engine instancesocket: any- The socket instance for the connecting player
Promise<string> | string | undefined- Player’s stable public identifier if authentication succeeds, or undefined to generate an ID automatically
string- Error message if authentication fails
publicId used by RPGJS and Signe room user collections. In MMORPG mode, the hook is called when the client connects to the lobby and again when it reconnects to map rooms. Send the same token on each connection and return the same id for the same account.
For browser MMORPG clients, pass the token through provideMmorpg({ query }); see Authentication.
Example:
Engine Runtime API
Theserver argument is an RpgServerEngine instance. It exposes stable helpers
for reading the current RPGJS room without depending on low-level Signe internals.
getCurrentRoom
Returns the current RPGJS room instance, such asLobbyRoom or RpgMap.
server.room, which is the low-level Signe/Party room
wrapper.
getCurrentRoomInfo
Returns stable metadata for the current room:id: full room id, such aslobby-1ormap-townkind:lobby,map, orunknownname: room id without the RPGJS prefixclassName: runtime room class nameplayersCount: number of players in the room when availableautoSync: whether automatic sync is enabledhasDatabase: whether the room exposes a database signal
getCurrentRoomId() and getCurrentRoomKind() when you only
need one value.
globalConfig
server.globalConfig is provided for compatibility with older server-engine
usage:
globalConfig. In lobby rooms or
before room initialization, it returns the last assigned value or {}.
app and io
server.app and server.io are optional compatibility handles. RPGJS v5 does
not create Express or socket.io automatically, but custom Node entries can assign
these properties when migrating older code: