express-ws-routes
Handle WebSocket connections using ws via express routes.
Differences from express-ws:
- Does not prefix the request URL with '.websocket' and instead uses a fake
WEBSOCKET
HTTP method - Websocket requests must be explicitly accepted (see ws's verifyClient option)
- Websocket requests can be rejected or the route can pass control to the next handler using
next()
(just like express routes) - Supports detached routers (i.e.
express.Router()
)
Installation
If using NPM v3, install express v4.5.0 or later:
npm install express@^4.5.0
npm install express-ws-routes
Usage
Express routes are used to verify and handle socket connections.
var express = ; // Create an express app with websocket supportvar app = ; // Add routes directly to the app... app; // ... or to detached routersvar router = express;router;app; // Reject requests using cb just like you would with ws's verifyClientapp; // Skip handlers by calling next(), just like normal routesapp; // Using app.listen will also create a require('ws').Servervar server = app; // The WebSocket server instance is available as a property of the HTTP serverserverwsServer;
Options
Module Name for 'express'
By default the express module name is express
but can be changed:
// Extend express using a specific module name, instead of 'express'var app = moduleName: 'my-custom-express' // i.e. require('my-custom-express');
Fake HTTP Method
By default the fake HTTP method is WEBSOCKET
but can be changed:
// Use a different web socketvar app = methodName: 'WS'; // Note: Method for attaching handlers will always be lowercaseapp;
Manual Setup
Instead of using the helper method you can do the following:
var options = {}; // See 'Options' abovevar expressWs = ;var app = expressWs; var server = http;serverwsServer = expressWs;server;
License
The MIT License (MIT)
Copyright (c) 2015 Andre Mekkawi <github@andremekkawi.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.