TypeScript TypeScript SDK

The JavaScript/TypeScript SDK is engine agnostic and can be used with any game engine or framework that supports WebSockets, such as:

  • Modern web browsers (and mobile web browsers)
  • Node.js
  • Bun
  • Electron
  • React Native
  • Engines/Frameworks: Phaser, PlayCanvas, React, Pixi.js, Babylon.js, THREE.js, KAPLAY, etc.

Adding the TypeScript SDK to your project

This is the preffered method if you’re using a build tool (vite, webpack, rollup, or similar)

npm install --save colyseus.js

Room’s .state type safety and auto-completion

The TypeScript SDK provides type safety and auto-completion for your state structures. There are two ways to enable this feature:

You may use import type to import only the type of the state from your server code.

import { Client } from "colyseus.js";
import type { MyState } from "../server/path/MyState";
 
const client = new Client("http://localhost:2567");
 
client.joinOrCreate<MyState>(...)
client.create<MyState>(...)
client.join<MyState>(...)
client.joinById<MyState>(...)
client.reconnect<MyState>(...)

2. Providing the concrete class reference:

In some cases, you may want to import the concrete implementation, to be able to re-use methods implemented in the server in the client.

import { MyState } from "../server/path/MyState"
 
client.joinOrCreate("my_room", {}, MyState);
Last updated on