Deployment
⚠️

Consider using Colyseus Cloud to easily deploy and scale your Colyseus servers.

Deploying Colyseus is no different than deploying a regular Node.js application - see a good article here. However, scaling Colyseus to use multiple processes requires extra steps - see Scalability.

Deploying your Colyseus server

Self-hosting on Vultr

A pre-configured Colyseus server is available on Vultr Marketplace. It’s a good option if you want to self-host your Colyseus server.

This server is configured with:

  • Node.js LTS
  • PM2
  • Nginx
  • FREE colyseus.dev subdomain with SSL (Let’s Encrypt)

Follow the instructions at Vultr Marketplace to get your server up and running.


Nginx configuration

When self-hosting, it is recommended to use nginx and pm2 in your production environment.

Nginx configuration

/etc/nginx/sites-available/yourdomain.com
server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://localhost:2567;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_read_timeout 86400s;
        proxy_send_timeout 86400s;
        include proxy_params;
    }
}

Nginx configuration with SSL

It’s recommended to acquire your certificate from LetsEncrypt.

/etc/nginx/sites-available/yourdomain.com
server {
    listen 80;
    listen 443 ssl;
    server_name yourdomain.com;

    ssl_certificate /path/to/your/cert.crt;
    ssl_certificate_key /path/to/your/cert.key;

    location / {
        proxy_pass http://localhost:2567;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_read_timeout 86400s;
        proxy_send_timeout 86400s;
        include proxy_params;
    }
}

Apache configuration

Here’s how to use Apache as a proxy to your Node.js Colyseus app. (Thanks tomkleine!)

Install the required Apache modules:

Terminal
sudo a2enmod ssl
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_html
sudo a2enmod proxy_wstunnel

Virtual host configuration:

/etc/apache2/sites-available/yourdomain.com.conf
<VirtualHost *:80>
    ServerName servername.xyz
    # Redirect all requests received from port 80 to the HTTPS variant (force ssl)
    RewriteEngine On
    RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>

<VirtualHost *:443>
    ServerName servername.xyz

    # enable SSL
    SSLEngine On
    SSLCertificateFile          /PATH/TO/CERT/FILE
    SSLCertificateKeyFile       /PATH/TO/PRIVATE/KEY/FILE

    #
    # setup the proxy to forward websocket requests properly to a normal websocket
    # and vice versa, so there's no need to change the colyseus library or the
    # server for that matter
    #
    # note: this proxy automatically converts the secure websocket (wss)

    RewriteEngine On
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$           [NC,OR]
    RewriteCond %{HTTP:CONNECTION} ^Upgrade$          [NC]
    RewriteRule .* ws://127.0.0.1:APP-PORT-HERE%{REQUEST_URI}  [P,QSA,L]

    # setup the proxy to forward all https requests to http backend
    # (also automatic conversion from https to http and vice versa)

    ProxyPass "/" "http://localhost:APP-PORT-HERE/"
    ProxyPassReverse "/" "http://localhost:APP-PORT-HERE/"

</VirtualHost>

Docker

Requirements

  • Download and install Docker
  • package.json and package-lock.json are in the project.
  • Set up the npm start command so it starts the server

Create Dockerfile in the root of the Colyseus project

Dockerfile
FROM node:22
 
ENV PORT 8080
 
WORKDIR /usr/src/app
 
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
 
RUN npm ci
# run this for production
# npm ci --only=production
 
COPY . .
 
EXPOSE 8080
 
CMD [ "npm", "start" ]

Create .dockerignore file in the same directory

.dockerignore
node_modules
npm-debug.log

This will prevent your local modules and debug logs from being copied onto your Docker image and possibly overwriting modules installed within your image.

Build Docker Image

Go to the directory that has your Dockerfile and run the following command to build the Docker image. The -t flag lets you tag your image so it’s easier to find later using the docker images command:

Terminal
docker build -t <your username>/colyseus-server .

Confirm Image Build

Your image will now be listed by Docker with following command:

Terminal
docker images

Output:

# Example
REPOSITORY                      TAG        ID              CREATED
node                            14         1934b0b038d1    About a minute ago
<your username>/colseus-server    latest     d64d3505b0d2    About a minute ago

Run the Docker Image

Run the Docker Image with following command:

Terminal
docker run -p 8080:8080 -d <your username>/colyseus-server

Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port inside the container.

Done

Done, now you can connect to the server with localhost:8080

More information:


Heroku

Heroku can be used for prototyping. You can deploy the colyseus-examples project on it by hitting this button:

Keep in mind that scaling to multiple processes and/or nodes on Heroku is not supported out of the box, therefore we don’t recommend using it in a production environment.

Deploy

Important: Make sure to set the environment variable NPM_CONFIG_PRODUCTION=false in order to use dev-dependencies in your deployment.

Last updated on