This example demonstrates how to use @colyseus/schema
with Socket.IO for efficient real-time state synchronization.
- Easy to Use: Clean API similar to regular Socket.IO
- Efficient State Sync: Only sends what changed, not the entire state
- Type Safety: TypeScript support with schema decorators - catches errors at compile time
- Reactive Callbacks: Listen to specific property changes
This example uses a single Encoder
for all connections. You may extend it to support a multiple Encoder
‘s per Socket.io Room. As @colyseus/schema
is in-memory and single-threaded, it is not possible to scale the same Encoder
between multiple servers or processes. If you're interested in scaling this approach, you can check how to scale Colyseus applications (without Socket.IO).
-
Install dependencies:
npm install
-
Run the server:
npm start
-
Start the client:
npm run client
- Defines
Player
andGameState
classes with@colyseus/schema
decorators - Provides type safety and efficient serialization
- Uses
Encoder
to track and send only state changes
- Uses
Decoder
to apply state updates - Reactive callbacks respond to specific changes
The result is a robust foundation for real-time multiplayer applications.
MIT