express-uws
WebSocket endpoints for Express applications. Lets you define WebSocket endpoints like any other type of route, and applies regular Express middleware. The WebSocket support is implemented with the help of the uws library. Identical to express-ws
in every way, except we are using uws for the underlying websocket implementation.
Installation
npm install --save express-uws
Usage
Full documentation can be found in the API section below. This section only shows a brief example.
Add this line to your Express application:
var expressWs = app;
Important: Make sure to set up the express-uws
module like above before loading or defining your routers! Otherwise, express-uws
won't get a chance to set up support for Express routers, and you might run into an error along the lines of router.ws is not a function
.
After setting up express-uws
, you will be able to add WebSocket routes (almost) the same way you add other routes. The following snippet sets up a simple echo server at /echo
. The ws
parameter is an instance of the WebSocket class described here.
app;
It works with routers, too, this time at /ws-stuff/echo
:
var router = express; router; app;
Full example
var express = ;var app = ;var expressWs = app; app; app; app; app;
API
expressWs(app, server, options)
Sets up express-uws
on the specified app
. This will modify the global Router prototype for Express as well - see the leaveRouterUntouched
option for more information on disabling this.
- app: The Express application to set up
express-uws
on. - server: Optional. When using a custom
http.Server
, you should pass it in here, so thatexpress-uws
can use it to set up the WebSocket upgrade handlers. If you don't specify aserver
, you will only be able to use it with the server that is created automatically when you callapp.listen
. - options: Optional. An object containing further options.
- leaveRouterUntouched: Set this to
true
to keepexpress-uws
from modifying the Router prototype. You will have to manuallyapplyTo
every Router that you wish to make.ws
available on, when this is enabled. - wsOptions: Options object passed to WebSocketServer constructor. Necessary for any ws specific features.
- leaveRouterUntouched: Set this to
This function will return a new express-uws
API object, which will be referred to as wsInstance
in the rest of the documentation.
wsInstance.app
This property contains the app
that express-uws
was set up on.
wsInstance.getWss()
Returns the underlying WebSocket server/handler. You can use wsInstance.getWss().clients
to obtain a list of all the connected WebSocket clients for this server.
Note that this list will include all clients, not just those for a specific route - this means that it's often not a good idea to use this for broadcasts, for example.
wsInstance.applyTo(router)
Sets up express-uws
on the given router
(or other Router-like object). You will only need this in two scenarios:
- You have enabled
options.leaveRouterUntouched
, or - You are using a custom router that is not based on the express.Router prototype.
In most cases, you won't need this at all.
Development
This module is written in ES6, and uses Babel for compilation. What this means in practice:
- The source code lives in the
src/
directory. - After changing this code, make sure to run
npm run build
to compile it.