Class UWSProxy

A proxy based on uWebSockets.js. Allow for compatibility between uWebSockets.js and any HTTP server.

It is useful in restricted server environment like clouds where you can't set up a proxy, or if you don't want to use a proxy and need uWebSockets.js to work on the same port as any other http server you're already using like express, nestjs, fastify, etc.

Example with express:

const http = require('http');
const express = require('express');
const uWebSockets = require('uWebSockets.js');

const {
UWSProxy,
createUWSConfig,
createHTTPConfig
} = require('uws-reverse-proxy');

const port = process.env.PORT || 80;

const proxy = new UWSProxy(
createUWSConfig(
uWebSockets,
{ port }
)
);

const expressApp = express();
expressApp.listen(
proxy.http.port,
proxy.http.host,
() => console.log(`HTTP Server listening at ${proxy.http.protocol}://${proxy.http.host}:${proxy.http.port}`)
);

proxy.uws.server.ws({
upgrade : () => {
//...
},
//...
});

proxy.uws.server.listen('0.0.0.0', port, listening => {
if(listening){
console.log(`uWebSockets.js listening on port 0.0.0.0:${port}`);
}else{
console.error(`Unable to listen on port 0.0.0.0:${port}!`);
}
});

See

More examples in the examples repository

Hierarchy

  • UWSProxy

Constructors

Properties

_httpClient: Client
_httpConfig: UWSProxyHTTPConfig
_uwsConfig: UWSProxyUWSConfig

Accessors

Methods

  • Construct a valid error response based on the provided error. The error response will be sent (if possible) to the client. To change any of the default response, use a UWSProxyErrorHandler callback

    Returns

    Parameters

    • error: Error

      The error we want to build a response upon

    Returns UWSProxyErrorResponse

  • Will try to respond to an error, either by using a UWSProxyErrorHandler (if any) or by sending a default error response.

    Parameters

    • error: Error

      The error that have been detected.

    • uwsResponse: UWSResponse

      The response we want to write into.

    • request: UWSDecodedRequest

      Informations about the current request.

    Returns void

  • Make an attempt to send an UWSProxyErrorResponse to the client. This may produce no result if the UWSResponse have been aborted/closed already. This method will just fail silently if it happens.

    Parameters

    Returns void

  • Try to use the provided UWSProxyErrorHandler if it was specified in UWSProxyOpts at UWSProxy creation.

    Returns

    False if no handler is defined, true otherwise

    Parameters

    • uwsResponse: UWSResponse

      The response we want to send an error into.

    • error: Error

      The raw error that have been detected.

    • request: UWSDecodedRequest

      Informations about the current request.

    Returns boolean

  • Attach routes listeners to uWebSocket to start proxying.

    Important

    This action can't be undone. uWebSockets.js do not allow listeners removal.

    Returns void

  • Create a valid httpConfiguration. The purpose of this method is to emit warnings or throw errors if the configuration doesn't seem valid.

    It's also meant to be a helper for further updates, where new configuration options will be added.

    using this method to configure UWSProxy is strongly recommended.

    Returns

    Parameters

    Returns UWSProxyHTTPConfig

Generated using TypeDoc