@irtio/server
v4.8.0
Published
irtio room-file API: defineRoom, handler and ctx types
Downloads
3,213
Readme
@irtio/server
Write multiplayer room logic with typed state, lifecycle handlers, validation, and RPCs.
defineRoom produces the room definition that the irtio CLI runs and deploys.
npm install @irtio/server @irtio/schema
npm install --save-dev irtioDefine a room
Create irtio/schema.ts:
import { defineSchema, entity, f32 } from '@irtio/schema';
export const schema = defineSchema(
{ players: entity({ x: f32, y: f32 }) },
{ roles: ['player'] as const },
);Create irtio/room.ts:
import { defineRoom } from '@irtio/server';
import { schema } from './schema.js';
export default defineRoom(schema, {
mode: 'tick',
tickRate: 20,
tick() {},
onJoin(state, ctx) {
if (ctx.reconnecting) return;
state.players.add(ctx.clientId, { x: 0, y: 0 }, { owner: ctx.clientId });
},
onLeave(state, ctx) {
state.players.remove(ctx.clientId);
},
});npx irtio devEach joining client gets a player record it owns. Other clients receive its updates. Use the client SDK to join and draw the room.
Choose where logic runs
| Configuration | Purpose |
| --- | --- |
| mode: 'tick', tickRate | Run a fixed-rate simulation; tickRate is ticks per second |
| mode: 'event' | Process incoming actions without a continuous tick loop |
| onAdmit | Admit or refuse a join before allocating a seat |
| onJoin, onLeave | Create and remove per-connection state |
| validate | Check client-owned writes against your game rules |
| rpc | Implement the server RPCs declared in your schema |
| onWake | Restore application behavior after hibernation |
Room handlers are synchronous. Start asynchronous storage operations and handle their results in
continuations. Use ctx.deny(reason) for an expected RPC refusal, and keep durable values in
schema state or player storage rather than module variables.
The room configuration reference lists every option. The server room API covers handler contexts, storage, timers, messages, and errors.
