Colyseus: Importing Schemas from other module doesn't work.

Created on 10 Jan 2020  路  6Comments  路  Source: colyseus/colyseus

My setup:
-typescript server and typescript client as 2 different projects, importing a common third project that contains shared/common stuff, especially the data model and the Schema, and exposes them as es6 modules

Even with a simple Schema such as:

export class TestSchema extends Schema {
    @type("number")
    whatever = 2;
}

When a client joins the server, i get the error on the client:

JOIN ERROR { Error: SchemaSerializer error. See: https://docs.colyseus.io/migrating/0.10/#new-default-serializer
    at new MatchMakeError (/home/stefano/project-x/project-x-webclient/node_modules/colyseus.js/lib/Client.js:58:28)
    at Client.<anonymous> (/home/stefano/project-x/project-x-webclient/node_modules/colyseus.js/lib/Client.js:182:35)
    at step (/home/stefano/project-x/project-x-webclient/node_modules/colyseus.js/lib/Client.js:45:23)
    at Object.next (/home/stefano/project-x/project-x-webclient/node_modules/colyseus.js/lib/Client.js:26:53)
    at fulfilled (/home/stefano/project-x/project-x-webclient/node_modules/colyseus.js/lib/Client.js:17:58)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7) code: 4213 }

and on the server:

Error: SchemaSerializer error. See: https://docs.colyseus.io/migrating/0.10/#new-default-serializer
    at SchemaSerializer.reset (/home/stefano/project-x/project-x-server/node_modules/colyseus/lib/serializer/SchemaSerializer.js:13:19)
    at GameRoom.setState (/home/stefano/project-x/project-x-server/node_modules/colyseus/lib/Room.js:108:26)
    at GameRoom.onCreate (/home/stefano/project-x/project-x-server/src/GameRoom.ts:12:14)
    at /home/stefano/project-x/project-x-server/node_modules/colyseus/lib/MatchMaker.js:214:28
    at Generator.next (<anonymous>)
    at /home/stefano/project-x/project-x-server/node_modules/colyseus/lib/MatchMaker.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (/home/stefano/project-x/project-x-server/node_modules/colyseus/lib/MatchMaker.js:3:12)
    at createRoom (/home/stefano/project-x/project-x-server/node_modules/colyseus/lib/MatchMaker.js:200:12)
    at Object.<anonymous> (/home/stefano/project-x/project-x-server/node_modules/colyseus/lib/MatchMaker.js:44:30)

If the TestSchema class is defined in the server project, no error is encountered, it only triggers when I import it from the common project.
The error fails on a instanceof check, I guess because the server and the common project have 2 different versions of the Schema class and therefore that check fails.

enhancement

All 6 comments

Hi @MastroLindus, as you mentioned, this is a version conflict of the @colyseus/schema (your package.json / what version colyseus is expecting).

This is what I always recommend doing:

rm -R node_modules
rm package-lock.json
npm install

Let me know if that works for you.

I'll make sure this error does not happen anymore in the next version (0.12.x)

Hi @endel , first of all thank you for such a fast response, and also for building this framework. It is very promising!

Regarding the question: I don't think it is a version conflict. My core package and my server package have the same 0.5.22 version of @colyseus/schema.
Actually my core package depends directly on @colyseus/schema, while the server only depends on "colyseus", but this one eventually depends as well on @colyseus/schema.

I think the problem is that even if the version is exactly the same, since they are imported twice from different projects and two different fileSystem paths, the node runtime will actually import the module twice, and therefore the instanceof check won't work (the classes are identical, but there are two separate instances in memory)

MastroLindus is right. I stumbled upon the same issue last week while I was messing about with npm link. In my case, my whole "base" project is a monorepo (Lerna managed), so both client and server side are in one dir, using the same "shared" node_modules. Then i have seperate project for the actual game, which is importing the "base" client and server libs. Maybe that could be your temporary solution?

Can you try out colyseus@alpha / colyseus.js@alpha? (I'm going to release version 0.12.x soon, it shouldn't have any breaking change besides what's described here https://deploy-preview-29--colyseus-docs.netlify.com/migrating/0.12/)

New version fixed it.
I guess that using some js bundlers it should be possible to detect if the same module with the same version gets imported twice and ensure that only one copy is bundled, and that also should fix this issue. However the newer code also works, thanks!, Feel free to close the issue or keep it around until 0.12.x is stable!

Closing as it seems to be resolved now. Let me know if you still have any problems with it!

Was this page helpful?
0 / 5 - 0 ratings