Skip to content
πŸ”€βš” Proxy and Service Discovery for Colyseus (0.10+)
TypeScript JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin
test
.env update README. move files to root directory. add dotenv dependency. Apr 7, 2019
.gitignore
LICENSE
README.md
architecture.png update README. move files to root directory. add dotenv dependency. Apr 7, 2019
discovery.ts update README. move files to root directory. add dotenv dependency. Apr 7, 2019
manual_hardcoded.ts fix publishing on npm Apr 8, 2019
package.json allow to deploy colyseus outside the root path Apr 10, 2019
proxy.ts
tsconfig.json

README.md

@colyseus/proxy

Proxy and Service Discovery for Colyseus

Architecture representation

Running the Proxy

Clone, this project and install its dependencies:

git clone https://github.com/colyseus/proxy.git
cd proxy
npm install

Edit the .env file to meet your needs:

  • HOST is your public domain name.
  • PORT is the port the proxy will be running on.
  • REDIS_URL is the path to the same Redis instance you're using on Colyseus' processes.

Start the proxy server:

npx ts-node proxy.ts

Configuring Colyseus + PM2

  • Configure RedisPresence
  • Configure MongooseDriver
  • Bind each instance of the server on a different port
  • Use PM2 to manage Colyseus instances
import { Server, RedisPresence } from "colyseus";
import { MongooseDriver } from "colyseus/lib/matchmaker/drivers/MongooseDriver"

// binds each instance of the server on a different port.
const PORT = Number(process.env.PORT) + Number(process.env.NODE_APP_INSTANCE);

const gameServer = new Server({
    presence: new RedisPresence({
        url: "redis://127.0.0.1:6379/0"
    }),
    driver: new MongooseDriver(),
})

gameServer.listen(PORT);
console.log("Listening on", PORT);

It's recommended to use PM2 to manage your server instances. PM2 allows to scale Node.js processes up and down within your server.

npm install -g pm2

Use the following ecosystem.config.js configuration:

// ecosystem.config.js
const os = require('os');
module.exports = {
    apps: [{
        port        : 8080,
        name        : "colyseus",
        script      : "lib/index.js", // your entrypoint file
        watch       : true,           // optional
        instances   : os.cpus().length,
        exec_mode   : 'fork',         // IMPORTANT: do not use cluster mode.
        env: {
            DEBUG: "colyseus:errors",
            NODE_ENV: "production",
        }
    }]
}

Now you're ready to start multiple Colyseus proceses.

pm2 start

If you're using TypeScript, compile your project before running pm2 start, via npx tsc.

You should see the following output, depending on the amount of processes your server have:

[PM2][WARN] Applications colyseus not running, starting...
[PM2] App [colyseus] launched (2 instances)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Name     β”‚ id β”‚ mode β”‚ status β”‚ β†Ί β”‚ cpu β”‚ memory    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ colyseus β”‚ 0  β”‚ fork β”‚ online β”‚ 0 β”‚ 0%  β”‚ 15.4 MB   β”‚
β”‚ colyseus β”‚ 1  β”‚ fork β”‚ online β”‚ 0 β”‚ 0%  β”‚ 12.3 MB   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Use `pm2 show <id|name>` to get more details about an app

Now, run pm2 logs to check if you don't have any errors.

LICENSE

MIT

You can’t perform that action at this time.