Server APITransportWebSocket (Default)

WebSocket Transport (via ws)

The WebSocketTransport with its default options is going to be used automatically if no transport is provided. You may customize its options by providing a custom WebSocketTransport instance.

The underlying library is websockets/ws - a simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js

Example

app.config.ts
import config from "@colyseus/tools";
import { WebSocketTransport } from "@colyseus/ws-transport"
 
export default config({
  // ...
 
  initializeTransport: function(opts) {
    return new WebSocketTransport({
      ...opts,
      pingInterval: 6000,
      pingMaxRetries: 4,
      maxPayload: 1024 * 1024 * 1, // 1MB Max Payload
    });
  },
 
  // ...
});

Available options

options.server

A Node.js http server instance to re-use for the WebSocket server. Useful when you’d like to use Express along with Colyseus.

app.config.ts
import { createServer } from "http";
import { Server } from "@colyseus/core";
import { WebSocketTransport } from "@colyseus/ws-transport"
 
const server = createServer(); // create the http server manually
 
const gameServer = new Server({
  transport: new WebSocketTransport({
    server // provide the custom server for `WebSocketTransport`
  })
});

By not providing this option, an http server is going to be created automatically for you.


options.pingInterval

Number of milliseconds for the server to “ping” the clients.

The clients are going to be forcibly disconnected if they can’t respond after pingMaxRetries retries.

Default: 3000


options.pingMaxRetries

Maximum allowed number of pings without a response.

Default: 2


options.maxPayload

Maximum payload clients can send per message to the server.

Default: 4096 (4kb)


options.verifyClient

This method happens before WebSocket handshake. If verifyClient is not set then the handshake is automatically accepted.

  • info (Object)

    • origin (String) The value in the Origin header indicated by the client.
    • req (http.IncomingMessage) The client HTTP GET request.
    • secure (Boolean) true if req.connection.authorized or req.connection.encrypted is set.
  • next (Function) A callback that must be called by the user upon inspection of the info fields. Arguments in this callback are:

    • result (Boolean) Whether or not to accept the handshake.
    • code (Number) When result is false this field determines the HTTP error status code to be sent to the client.
    • name (String) When result is false this field determines the HTTP reason phrase.
Last updated on