Skip to content

Cocos Creator 3

Cocos Creator is a Cross-Platform 2D/3D Game Creation Tool.

The Colyseus SDK for Cocos Creator works in the same way as the JavaScript SDK. The major difference is how to import it from a TypeScript Component asset from Cocos Creator.

Looking for an example?

Take a look at the Tic Tac Toe Example.

1. Install the extension

2. Update your tsconfig.json file

Make sure you have "esModuleInterop": true under "compilerOptions":

  // ...

  /* Add your custom configuration here. */
  "compilerOptions": {
    // ...
    "esModuleInterop": true
    // ...
  }
  // ...

3. Importing from the extension

You must import Colyseus from its extension path:

import Colyseus from 'db://colyseus-sdk/colyseus.js';

Below is a more elaborate example you can copy and paste into your project.

Save the contents below as NetworkManager.ts under your assets folder.

import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

import Colyseus from 'db://colyseus-sdk/colyseus.js';

@ccclass('NetworkManager')
export class NetworkManager extends Component {
    @property hostname = "localhost";
    @property port = 2567;
    @property useSSL = false;

    client!: Colyseus.Client;
    room!: Colyseus.Room;

    start () {
        // Instantiate Colyseus Client
        // connects into (ws|wss)://hostname[:port]
        this.client = new Colyseus.Client(`${this.useSSL ? "wss" : "ws"}://${this.hostname}${([443, 80].includes(this.port) || this.useSSL) ? "" : `:${this.port}`}`);

        // Connect into the room
        this.connect();
    }

    async connect() { 
        try {
            this.room = await this.client.joinOrCreate("my_room");

            console.log("joined successfully!");
            console.log("user's sessionId:", this.room.sessionId);

            this.room.onStateChange((state) => {
                console.log("onStateChange: ", state);
            });

            this.room.onLeave((code) => {
                console.log("onLeave:", code);
            });

        } catch (e) {
            console.error(e);
        }
    }
}

You can now attach NetworkManager as a Component on any Node from Cocos Creator.

Because we're using the @property decorator above, you can edit the hostname, port and useSSL from Cocos Creator editor:

Colyseus SDK on TypeScript Component


Alternative: How to manually add the colyseus.js file into your project (without the extension)

  • Download the latest colyseus-js-client.zip release from GitHub.
  • Unzip the colyseus-js-client.zip file into the assets/ folder.
  • Move both colyseus.js and colyseus.d.ts files into your Cocos Creator project's scripts folder.
  • Click on the colyseus.js file from the Assets panel, and enable to "Import As Plugin" (see image below)
  • TypeScript: require it using import Colyseus from "./colyseus.js";
  • JavaScript: require it using const Colyseus = require("./colyseus.js");

Import as plugin

Back to top