# express-ws-routes

> Handle WebSocket connections using express routes.

Latest version **1.1.0** (published 2016-01-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-ws-routes
pnpm add express-ws-routes
yarn add express-ws-routes
bun add express-ws-routes
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2016-01-24 |
| First published | 2015-10-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/express-ws-routes) |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 20 |
| Author | Andre Mekkawi |
| Maintainers | amekkawi |
| Keywords | express, ws, websocket |

## Links

- npm: https://www.npmjs.com/package/express-ws-routes
- Repository: https://github.com/amekkawi/express-ws-routes
- Issues: https://github.com/amekkawi/express-ws-routes/issues
- npm.io page: https://npm.io/package/express-ws-routes

## Dependencies (3)

- [ws](https://npm.io/package/ws.md) ^0.8.0 || ^1.0.0
- [debug](https://npm.io/package/debug.md) ^2.2.0
- [array-flatten](https://npm.io/package/array-flatten.md) ^1.1.1

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2016-01-24
- 1.0.2 — 2015-10-19
- 1.0.1 — 2015-10-19
- 1.0.0 — 2015-10-19

## README

# express-ws-routes #

Handle WebSocket connections using [ws](https://www.npmjs.com/package/ws) via express routes.

Differences from [express-ws](https://www.npmjs.com/package/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.

```javascript
var express = require('express');

// Create an express app with websocket support
var app = require('express-ws-routes')();

// Add routes directly to the app... 
app.websocket('/myurl', function(info, cb, next) {
	// `info` is the same as ws's verifyClient
	console.log(
		'ws req from %s using origin %s',
		info.req.originalUrl || info.req.url,
		info.origin
	);

	// Accept connections by passing a function to cb that will handle the connected websocket
	cb(function(socket) {
		socket.send('connected!');
	});
});

// ... or to detached routers
var router = express.Router();
router.websocket('/sub/path', function(info, cb, next) {
	cb(function(socket) {
		socket.send('connected!');
	});
});
app.use('/attachment/path', router);

// Reject requests using cb just like you would with ws's verifyClient
app.websocket('/bad/path', function(info, cb, next) {
	cb(false);
	
	// Using the optional arguments...
	//cb(false, 401);
	//cb(false, 401, 'No access!');
});

// Skip handlers by calling next(), just like normal routes
app.websocket('/skipped', function(info, cb, next) {
	console.log('Skipped!');
	next();
});

// Using app.listen will also create a require('ws').Server
var server = app.listen(8080, function() {
	console.log('Server listening on port 8080...');
});

// The WebSocket server instance is available as a property of the HTTP server
server.wsServer.on('connection', function(socket) {
	console.log('connection to %s', socket.upgradeReq.url);
});
```

## Options ##

### Module Name for 'express'

By default the express module name is `express` but can be changed:

```javascript
// Extend express using a specific module name, instead of 'express'
var app = require('express-ws-routes')({
	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:

```javascript
// Use a different web socket
var app = require('express-ws-routes')({
	methodName: 'WS'
});

// Note: Method for attaching handlers will always be lowercase
app.ws(function(info, cb, next) {
	// ...
});
```

## Manual Setup ##

Instead of using the helper method you can do the following:

```javascript
var options = {}; // See 'Options' above
var expressWs = require('express-ws-routes');
var app = expressWs.extendExpress(options)();

var server = http.createServer(app);
server.wsServer = expressWs.createWebSocketServer(server, app, options);
server.listen(8080, function() {
	console.log('Server listening on port 8080...');
});
```

## License ##

The MIT License (MIT)

Copyright (c) 2015 Andre Mekkawi &lt;github@andremekkawi.com&gt;

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.

---
_Source: https://npm.io/package/express-ws-routes · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
