State synchronization across server nodes. #578
-
Does Colyseus synchronize state across servers? Suppose if there's two servers Server A and Server B running on ports 2567 and 2568 respectively. They both share the same server codebase. Please refer to the code snippets below. Server A and B connects to a Redis docker container running on port index.ts: import "dotenv/config.js";
const port = process.env.COLYSEUS_PORT
? Number(process.env.COLYSEUS_PORT)
: 2567;
import { RedisPresence, Room, Server } from "colyseus";
import { MapSchema, Schema, type } from "@colyseus/schema";
console.log("COLYSEUS_PORT", process.env.COLYSEUS_PORT);
console.log("REDIS_HOST", process.env.REDIS_HOST);
const serverOptions = {
presence: new RedisPresence({
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT ? Number(process.env.REDIS_PORT) : 6379,
}),
};
class Player extends Schema {
@type("string") sessionId: string = "";
@type("number") x: number = 0;
@type("number") y: number = 0;
}
class RoomState extends Schema {
@type({ map: Player }) players = new MapSchema<Player>();
}
class TestRoom extends Room<RoomState> {
maxClients = 4;
onCreate(options: any) {
console.log("TestRoom created!", options);
this.setState(new RoomState());
this.onMessage("move", this.onMove.bind(this));
}
onJoin(client: any, options: any) {
const player = new Player();
player.sessionId = client.sessionId;
this.state.players.set(client.sessionId, player);
}
onMove(client: any, coors: {x: number, y: number}) {
console.log("TestRoom received coords from", client.sessionId, ":", coors);
const player = this.state.players.get(client.sessionId);
if (player) {
player.x = coors.x;
player.y = coors.y;
}
// print out all players coords
this.state.players.forEach((player) => {
console.log(player.sessionId, player.x, player.y);
});
}
}
const gameServer = new Server(serverOptions);
gameServer.define("testroom", TestRoom);
gameServer.listen(port); Server A Console Output $ NODE_ENV=development COLYSEUS_PORT=2567 ts-node src/index.ts
COLYSEUS_PORT 2567
REDIS_HOST 127.0.0.1
___ _
/ __\___ | |_ _ ___ ___ _ _ ___
/ / / _ \| | | | / __|/ _ \ | | / __|
/ /__| (_) | | |_| \__ \ __/ |_| \__ \
\____/\___/|_|\__, |___/\___|\__,_|___/
|___/
Multiplayer Framework for Node.js · Open-source
💖 Sponsor Colyseus on GitHub → https://github.com/sponsors/endel
🌟 Give it a star on GitHub → https://github.com/colyseus/colyseus
☁️ Deploy and scale your project on Colyseus Cloud → https://cloud.colyseus.io
Error: seat reservation expired.
at WebSocketServer.onConnection (C:\..\state-sync-test\server\node_modules\@colyseus\ws-transport\build\WebSocketTransport.js:102:15)
at WebSocketServer.emit (node:events:513:28)
at WebSocketServer.emit (node:domain:489:12)
at WebSocketServer.completeUpgrade (C:\..\state-sync-test\server\node_modules\ws\lib\websocket-server.js:358:5)
at WebSocketServer.handleUpgrade (C:\..\state-sync-test\server\node_modules\ws\lib\websocket-server.js:271:10)
at Server.upgrade (C:\..\state-sync-test\server\node_modules\ws\lib\websocket-server.js:107:16)
at Server.emit (node:events:513:28)
at Server.emit (node:domain:489:12)
at onParserExecuteCommon (node:_http_server:903:14)
at onParserExecute (node:_http_server:797:3)
TestRoom created! {}
TestRoom received coords from KzVK_m1Ou : { x: 2, y: 3 }
KzVK_m1Ou 2 3
TestRoom received coords from KzVK_m1Ou : { x: 66, y: 66 }
KzVK_m1Ou 66 66 Server B Console Output $ NODE_ENV=development COLYSEUS_PORT=2568 ts-node src/index.ts
COLYSEUS_PORT 2568
REDIS_HOST 127.0.0.1
___ _
/ __\___ | |_ _ ___ ___ _ _ ___
/ / / _ \| | | | / __|/ _ \ | | / __|
/ /__| (_) | | |_| \__ \ __/ |_| \__ \
\____/\___/|_|\__, |___/\___|\__,_|___/
|___/
Multiplayer Framework for Node.js · Open-source
💖 Sponsor Colyseus on GitHub → https://github.com/sponsors/endel
🌟 Give it a star on GitHub → https://github.com/colyseus/colyseus
☁️ Deploy and scale your project on Colyseus Cloud → https://cloud.colyseus.io
TestRoom created! {}
TestRoom received coords from Fmy5Mx9hT : { x: 4, y: 5 }
Fmy5Mx9hT 4 5
TestRoom received coords from Fmy5Mx9hT : { x: 6, y: 7 }
Fmy5Mx9hT 6 7
TestRoom received coords from Fmy5Mx9hT : { x: 77, y: 77 }
Fmy5Mx9hT 77 77 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 16 replies
-
Hi @bwasia, when using multiple processes you must also provide a This is required so the client-side makes the request directly to the node that created the room and has the room in memory. Each room and each process is stateful in Colyseus. Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Hey @endel, thank you for a superb library! I am wondering if there are any examples of syncing state across servers with Redis? I have read the documentation but am running into some difficulties getting things correctly setup. |
Beta Was this translation helpful? Give feedback.
There is no any sharing state across processes. To sync some state across the rooms you can use redis