Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates #139

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .eslintrc

This file was deleted.

43 changes: 43 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

module.exports = {
extends: 'airbnb/base',
overrides: [
{
files: '.eslintrc.js',
parserOptions: {
sourceType: 'script'
},
// airbnb's config here is too inflexible as it prevents strict mode
// even with scripts, so set to `safe`
// Changed: https://github.com/airbnb/javascript/pull/1962
rules: {
strict: ['error', 'safe']
}
},
{
files: ['examples/**'],
rules: {
'no-console': 0
}
},
{
files: ['test/**'],
env: {
mocha: true
},
globals: {
expect: true
},
rules: {
'no-console': 0,
// Is otherwise reporting in Atom ESLint linter (though not on command line)
'import/no-extraneous-dependencies': 0
}
}
],
rules: {
'comma-dangle': 0,
'no-param-reassign': 0
}
};
8 changes: 8 additions & 0 deletions .ncurc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
// Whitelist all for checking besides `peer` which indicates
// somewhat older versions of peer deps. we still support even
// while our devDeps point to a more recent version
"dep": "prod,dev,optional,bundle"
};
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
src/
test/
36 changes: 36 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
language: node_js
os: linux
dist: xenial

node_js:
- 14
- 12
- 10

notifications:
email: false

script:
- npm test
- 'if [ -n "${LINT-}" ]; then npm run lint; fi'

before_install:
- npm config set depth 0

before_script: >
node_version=$(node -v);
npm install --no-save "express@${EXPRESS}"

env:
jobs:
- EXPRESS=4
- EXPRESS=5.0.0-alpha.8
jobs:
fast_finish: true
include:
- node_js: 'lts/*'
env: LINT=true
# If Express exclusions needed by Node version
# exclude:
# - node_js: 10
# env: EXPRESS=7
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ __Full documentation can be found in the API section below. This section only sh
Add this line to your Express application:

```javascript
var expressWs = require('express-ws')(app);
const expressWs = require('express-ws')(app);
```

__Important: Make sure to set up the `express-ws` module like above *before* loading or defining your routers!__ Otherwise, `express-ws` 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-ws`, 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](https://github.com/websockets/ws/blob/master/doc/ws.md#class-websocket).

```javascript
app.ws('/echo', function(ws, req) {
ws.on('message', function(msg) {
app.ws('/echo', (ws, req) => {
ws.on('message', (msg) => {
ws.send(msg);
});
});
Expand All @@ -31,37 +31,37 @@ app.ws('/echo', function(ws, req) {
It works with routers, too, this time at `/ws-stuff/echo`:

```javascript
var router = express.Router();
const router = express.Router();

router.ws('/echo', function(ws, req) {
ws.on('message', function(msg) {
router.ws('/echo', (ws, req) => {
ws.on('message', (msg) => {
ws.send(msg);
});
});

app.use("/ws-stuff", router);
app.use('/ws-stuff', router);
```

## Full example

```javascript
var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);

app.use(function (req, res, next) {
app.use((req, res, next) => {
console.log('middleware');
req.testing = 'testing';
return next();
});

app.get('/', function(req, res, next){
app.get('/', (req, res, next) => {
console.log('get route', req.testing);
res.end();
});

app.ws('/', function(ws, req) {
ws.on('message', function(msg) {
app.ws('/', (ws, req) => {
ws.on('message', (msg) => {
console.log(msg);
});
console.log('socket', req.testing);
Expand All @@ -80,26 +80,26 @@ Sets up `express-ws` on the specified `app`. This will modify the global Router
* __server__: *Optional.* When using a custom `http.Server`, you should pass it in here, so that `express-ws` can use it to set up the WebSocket upgrade handlers. If you don't specify a `server`, you will only be able to use it with the server that is created automatically when you call `app.listen`.
* __options__: *Optional.* An object containing further options.
* __leaveRouterUntouched:__ Set this to `true` to keep `express-ws` from modifying the Router prototype. You will have to manually `applyTo` 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.
* __wsOptions:__ Options object passed to `ws.Server` constructor. Necessary for any ws specific features.

This function will return a new `express-ws` API object, which will be referred to as `wsInstance` in the rest of the documentation.
This function will return a new `express-ws` API object, which will be referred to as `expressWsInstance` in the rest of the documentation.

### wsInstance.app
### expressWsInstance.app

This property contains the `app` that `express-ws` was set up on.

### wsInstance.getWss()
### expressWsInstance.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.
Returns the underlying WebSocket server/handler. You can use `expressWsInstance.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)
### expressWsInstance.applyTo(router)

Sets up `express-ws` on the given `router` (or other Router-like object). You will only need this in two scenarios:

1. You have enabled `options.leaveRouterUntouched`, or
2. You are using a custom router that is not based on the express.Router prototype.
2. 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.

Expand Down
20 changes: 10 additions & 10 deletions examples/broadcast.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
var express = require('express');
var expressWs = require('..')
const express = require('express');
let expressWs = require('..');

var expressWs = expressWs(express());
var app = expressWs.app;
expressWs = expressWs(express());
const { app } = expressWs;

app.ws('/a', function(ws, req) {
app.ws('/a', (/* ws, req */) => {
});
var aWss = expressWs.getWss('/a');
const wss = expressWs.getWss();

app.ws('/b', function(ws, req) {
app.ws('/b', (/* ws, req */) => {
});

setInterval(function () {
aWss.clients.forEach(function (client) {
setInterval(() => {
wss.clients.forEach((client) => {
client.send('hello');
});
}, 5000);

app.listen(3000)
app.listen(3000);
26 changes: 13 additions & 13 deletions examples/https.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
var https = require('https');
var fs = require('fs');
const https = require('https');
const fs = require('fs');

var express = require('express');
var expressWs = require('..');
const express = require('express');
const expressWs = require('..');

var options = {
const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
var app = express();
var server = https.createServer(options, app);
var expressWs = expressWs(app, server);
const app = express();
const server = https.createServer(options, app);
expressWs(app, server);

app.use(function (req, res, next) {
app.use((req, res, next) => {
console.log('middleware');
req.testing = 'testing';
return next();
});

app.get('/', function(req, res, next){
app.get('/', (req, res /* , next */) => {
console.log('get route', req.testing);
res.end();
});

app.ws('/', function(ws, req) {
ws.on('message', function(msg) {
app.ws('/', (ws, req) => {
ws.on('message', (msg) => {
console.log(msg);
});
console.log('socket', req.testing);
});

server.listen(3000)
server.listen(3000);
18 changes: 9 additions & 9 deletions examples/params.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
var express = require('express');
var expressWs = require('..');
const express = require('express');
let expressWs = require('..');

var expressWs = expressWs(express());
var app = expressWs.app;
expressWs = expressWs(express());
const { app } = expressWs;

app.param('world', function (req, res, next, world) {
app.param('world', (req, res, next, world) => {
req.world = world || 'world';
return next();
});

app.get('/hello/:world', function(req, res, next){
app.get('/hello/:world', (req, res, next) => {
console.log('hello', req.world);
res.end();
next();
});

app.ws('/hello/:world', function(ws, req, next) {
ws.on('message', function(msg) {
app.ws('/hello/:world', (ws, req, next) => {
ws.on('message', (msg) => {
console.log(msg);
});
console.log('socket hello', req.world);
next();
});

app.listen(3000)
app.listen(3000);
18 changes: 9 additions & 9 deletions examples/simple.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
var express = require('express');
var expressWs = require('..');
const express = require('express');
let expressWs = require('..');

var expressWs = expressWs(express());
var app = expressWs.app;
expressWs = expressWs(express());
const { app } = expressWs;

app.use(function (req, res, next) {
app.use((req, res, next) => {
console.log('middleware');
req.testing = 'testing';
return next();
});

app.get('/', function(req, res, next){
app.get('/', (req, res /* , next */) => {
console.log('get route', req.testing);
res.end();
});

app.ws('/', function(ws, req) {
ws.on('message', function(msg) {
app.ws('/', (ws, req) => {
ws.on('message', (msg) => {
console.log(msg);
});
console.log('socket', req.testing);
});

app.listen(3000)
app.listen(3000);
Loading