Skip to content

JavaScript Client


The JavaScript client is compatible with:

Don't know NodeJS build-systems like webpack? Just save and drop the JavaScript distribution file into your project and ignore the import statements presented here in the documentation.


Installing the module

npm install --save colyseus.js

Connecting to server:

import * as Colyseus from "colyseus.js";

var client = new Colyseus.Client('ws://localhost:2567');

Joining to a room:

client.join("room_name").then(room => {
    console.log(room.sessionId, "joined",;
}).catch(e => {
    console.log("JOIN ERROR", e);

Room events

Room state has been updated:

room.onStateChange((state) => {
  console.log(, "has new state:", state);

Message broadcasted from server or directly to this client:

room.onMessage("message_type", (message) => {
  console.log(, "received on",, message);

Server error occurred:

room.onError((code, message) => {
  console.log(, "couldn't join",;

The client left the room:

room.onLeave((code) => {
  console.log(, "left",;

React Native compatibility

This client works with React Native. You need to install some aditional dependencies for compatibility and assign window.localStorage to AsyncStorage.

  • npm install buffer

// App.js
import { AsyncStorage } from 'react-native';
import { Buffer } from "buffer";
window.localStorage = AsyncStorage;
global.Buffer = Buffer;

Cocos Creator Instructions

  • Download the latest colyseus.js distribution file from GitHub.
  • Save it into your project's scripts folder.
  • Require it using const Colyseus = require('colyseus.js')