Skip to content

Overview

Colyseus currently have clients for the platforms:

Need a client for another platform? Share your interest on the discussion board!

Connecting to the Server

import * as Colyseus from "colyseus.js";
// ...

let client = new Colyseus.Client("ws://localhost:2567");
using Colyseus;
// ...

Client client = new Client("ws://localhost:2567");
local ColyseusClient = require("colyseus.client")
// ...

local client = ColyseusClient.new("ws://localhost:2567");
import io.colyseus.Client;
// ...

var client = new Client("ws://localhost:2567");

Methods

join (roomNameOrId: string, options: any)

Joins roomName. roomName can be either a room name or a roomId.

// joining a room by name
let room = client.join("battle");

// joining a room by id
let room = client.join("KRYAKzRo2");
// joining a room by name
Room room = client.Join("battle");

// joining a room by id
Room room = client.Join("KRYAKzRo2");
-- joining a room by name
local room = client:join("battle")

-- joining a room by id
local room = client:join("KRYAKzRo2")
// joining a room by name
var room = client.join("battle");

// joining a room by id
var room = client.join("KRYAKzRo2");

Tip

Use getAvailableRooms() to retrieve a list of roomId's available for joining.

rejoin (roomNameOrId: string, sessionId: string)

Reconnects the client into a room he was previously connected with.

Must be used along with allowReconnection() in the server-side.

let room = client.rejoin("battle", "SkNaHTazQ");
Room room = client.ReJoin("battle", "SkNaHTazQ");
local room = client:rejoin("battle", "SkNaHTazQ")
var room = client.rejoin("battle", "SkNaHTazQ");

getAvailableRooms (roomName: string)

List all available rooms to connect with the provided roomName. Locked rooms won't be listed.

client.getAvailableRooms("battle", function(rooms, err) {
  if (err) console.error(err);
  rooms.forEach(function(room) {
    console.log(room.roomId);
    console.log(room.clients);
    console.log(room.maxClients);
    console.log(room.metadata);
  });
});
client.GetAvailableRooms("battle", (RoomAvailable[] rooms) => {
  for (int i = 0; i < rooms.Length; i++) {
    Debug.Log(rooms[i].roomId);
    Debug.Log(rooms[i].clients);
    Debug.Log(rooms[i].maxClients);
    Debug.Log(rooms[i].metadata);
  }
);
client:get_available_rooms("battle", function(rooms, err)
  if (err) console.error(err);
  for i, rooms in pairs(rooms) do
    print(rooms[i].roomId)
    print(rooms[i].clients)
    print(rooms[i].maxClients)
    print(rooms[i].metadata)
  end
end);
client.getAvailableRooms("battle", function(rooms, ?err) {
  if (err != null) trace(err);
  for (room in rooms) {
    trace(room.roomId);
    trace(room.clients);
    trace(room.maxClients);
    trace(room.metadata);
  }
});

close ()

Close connection with the server.

client.close();
client.Close();
client:close()
client.close();

Events

onOpen

This event is triggered when the connection is accepted by the server.

client.onOpen.add(function() {
  console.log("connection is now open");
});
client.OnOpen += (object sender, EventArgs e) => {
  Debug.Log ("connection is now open");
}
client:on('open', function()
  print("connection is now open")
end)
client.onOpen = function() {
  trace("connection is now open");
};

onClose

This event is triggered when the connection is closed.

client.onClose.add(function() {
  console.log("connection has been closed");
});
client.OnClose += (object sender, EventArgs e) => {
  Debug.Log ("connection has been closed");
}
client:on('close', function()
  print("connection has been closed")
end)
client.onClose = function() {
  trace("connection has been closed");
};

onError

This event is triggered when some error occurs in the server.

client.onError.add(function(err) {
  console.log("something wrong happened", err);
});
client.OnError += (object sender, EventArgs e) => {
  Debug.Log ("something wrong happened");
}
client:on("error", function()
  print("something wrong happened")
end)
client.onError = function() {
  trace("something wrong happened");
};

Properties

id: string

Unique identifier for the client.

Note

The same client id can connect into the same room handler when joining from multiple browser tabs.