Server APITransportuWebSockets.js

uWebSockets.js

The uWebSockets.js generally performs better than the default WebSockets implementation, and is capable of handling more connections while using less resources. It is the recommended transport for production environments.

The underlying library uNetworking/uWebSockets.js is a C++ implementation of WebSockets that performs at least 10x that of Socket.IO, 8.5x that of Fastify. It makes up the core components of Bun and is the fastest standards compliant web server in the TechEmpower (not endorsed) benchmarks.

Installation

npm install --save @colyseus/uwebsockets-transport

Usage

app.config.ts
import { uWebSocketsTransport } from "@colyseus/uwebsockets-transport"
import config from "@colyseus/tools";
 
export default config({
  // ...
  initializeTransport: function() {
    return new uWebSocketsTransport({
      /* ...options */
    });
  },
 
  //
  // bind express routes
  //
  initializeExpress: (app) => {
 
    app.get("/hello", (req, res) => {
      res.json({ hello: "world!" });
    });
 
  },
  // ...
})

Available options

options.maxPayloadLength

Maximum length of received message. If a client tries to send you a message larger than this, the connection is immediately closed.

Default: 4096

options.idleTimeout

Maximum amount of seconds that may pass without sending or getting a message. Connection is closed if this timeout passes. Resolution (granularity) for timeouts are typically 4 seconds, rounded to closest. Disable by using 0.

Default: 120

options.sendPingsAutomatically

Whether or not we should automatically send pings to uphold a stable connection given idleTimeout.

Default: true

options.compression

What permessage-deflate compression to use. uWS.DISABLED, uWS.SHARED_COMPRESSOR or any of the uWS.DEDICATED_COMPRESSOR_xxxKB.

Default: uWS.DISABLED

options.maxBackpressure

Maximum length of allowed backpressure per socket when publishing or sending messages. Slow receivers with too high backpressure will be skipped until they catch up or timeout.

Default: 1024 * 1024

options.key_file_name

Path to the SSL key file. (for SSL termination through the Node.js application.)

options.cert_file_name

Path to the SSL certificate file. (for SSL termination through the Node.js application.)

options.passphrase

Password for the SSL file. (for SSL termination through the Node.js application.)

Last updated on