{"_id":"@a1motion/express-ws","_rev":"1-465707f9e1a85058336394fce780da68","name":"@a1motion/express-ws","dist-tags":{"latest":"5.0.2"},"versions":{"5.0.2":{"name":"@a1motion/express-ws","version":"5.0.2","description":"WebSocket endpoints for Express applications","main":"index","module":"src/index","scripts":{"lint":"eslint src/"},"author":{"name":"Henning Morud","email":"henning@morud.org"},"contributors":[{"name":"Jesús Leganés Combarro","email":"piranna@gmail.com"},{"name":"Sven Slootweg","email":"admin@cryto.net"},{"name":"Andrew Phillips","email":"theasp@gmail.com"},{"name":"Nicholas Schell","email":"nschell@gmail.com"},{"name":"Max Truxa","email":"dev@maxtruxa.com"},{"name":"Kræn Hansen","email":"mail@kraenhansen.dk"},{"name":"Alexis Tyler","email":"xo@wvvw.me"}],"license":"BSD-2-Clause","dependencies":{"esm":"^3.0.84","ws":"^7.1.2"},"peerDependencies":{"express":"^4.0.0 || ^5.0.0-alpha.1"},"engines":{"node":">=4.5.0"},"repository":{"type":"git","url":"https://github.com/HenningM/express-ws"},"keywords":["express","ws","websocket"],"bugs":{"url":"https://github.com/HenningM/express-ws/issues"},"homepage":"https://github.com/HenningM/express-ws","devDependencies":{"eslint":"^4.19.0","eslint-config-airbnb":"^14.1.0","eslint-plugin-import":"^2.12.0","express":"^5.0.0-alpha.6"},"licenseText":"Copyright (c) 2016, Henning Morud\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this\n  list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice,\n  this list of conditions and the following disclaimer in the documentation\n  and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\nOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n","_id":"@a1motion/express-ws@5.0.2","dist":{"shasum":"3d7a6402921eb42b934f3d462fdaaed156b73987","integrity":"sha512-lodLhnApT7eWSbcK5FOMJ0c83c2z0cvkmsSFrqWEy4E2EgaLlQWop62tDZ5XjSPRGIiSykD0NJgyQDAR67csAw==","tarball":"https://registry.npmjs.org/@a1motion/express-ws/-/express-ws-5.0.2.tgz","fileCount":12,"unpackedSize":9315,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJddvVECRA9TVsSAnZWagAAXpoP/ixc6zwEjaSOahMhxEEf\nC/+jw0xAuQhqNRp/9Ehc3oCx8JoQGSWYRkZoDeKqLIwgRv/Mqr7q0WAWXw5F\nKQDaPX2vGtpJ6yJs3pmzviv0BYUdR4Un3PZcJL9GGtFKWwg+k7Kkg68HFRXU\n/BSOZnXOtUnfQnV58Dk/Rqo+pumLPtYmUmXff4G4nfUTOJ22ZFAyjTl2SN3u\nHAsOSAz2TlpW6C5D6dl3dpDVx4IdJ56y8rVga/gNwRAZsNi4Sc/a1Fh9fs7k\nqzZhkj5ieCQkehwmhcKZ1KPT5DSkYRs08bov2GtW3a7Uu3h+9Zl33blpEf+8\nwgoMWKpTAev7cDNH186MqBU1UD6cT+pNb1cl9kBoqvAP22wWQIHDiBgoUJEq\nlMF8Cq+EXkjJvYYUslIwC8Nfygxmwekb2YES+tC9MEdipRvMDVd8DNZHtjlP\nbFIo27e847om27w9WumTyPIZeMi7hrtq+UTAMT9lUd4HfTXG0VahEwtMOXyk\nAchC8In2jhdErweuqfdG3nUaO9Y8UTUtkIJDBJZgQxjLbZYLeiFb1U+9Aa4z\nCZwj/9KA0W/XkjbsXYoEvXXhlu4VHlolzjTHrSGUXMkhicLfGvm2OdDhuzgx\nrqHwAL21hpGsKBZ5iDHJAuyH6iEe9TFi75yZ2lkik5R349fYHKCgU24qYVbe\n2ig2\r\n=b5dE\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCWVD+UOvHZEZAPEOTT8n8aDjCo6sK1SpCy3b8AIX5ItgIgXUz34dqH1aJ0h/g8sR1hnK69cFrL2igh4484H3B37pI="}]},"maintainers":[{"name":"dotconnor","email":"connor@dotconnor.com"}],"_npmUser":{"name":"dotconnor","email":"connor@dotconnor.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/express-ws_5.0.2_1568077124073_0.9252383062271812"},"_hasShrinkwrap":false}},"time":{"created":"2019-09-10T00:58:43.976Z","5.0.2":"2019-09-10T00:58:44.207Z","modified":"2022-04-04T10:52:52.642Z"},"maintainers":[{"name":"dotconnor","email":"connor@dotconnor.com"}],"description":"WebSocket endpoints for Express applications","homepage":"https://github.com/HenningM/express-ws","keywords":["express","ws","websocket"],"repository":{"type":"git","url":"https://github.com/HenningM/express-ws"},"contributors":[{"name":"Jesús Leganés Combarro","email":"piranna@gmail.com"},{"name":"Sven Slootweg","email":"admin@cryto.net"},{"name":"Andrew Phillips","email":"theasp@gmail.com"},{"name":"Nicholas Schell","email":"nschell@gmail.com"},{"name":"Max Truxa","email":"dev@maxtruxa.com"},{"name":"Kræn Hansen","email":"mail@kraenhansen.dk"},{"name":"Alexis Tyler","email":"xo@wvvw.me"}],"author":{"name":"Henning Morud","email":"henning@morud.org"},"bugs":{"url":"https://github.com/HenningM/express-ws/issues"},"license":"BSD-2-Clause","readme":"# express-ws [![Dependency Status](https://snyk.io/test/github/henningm/express-ws/badge.svg)](https://snyk.io/test/github/henningm/express-ws)\n\n[WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) endpoints for [Express](http://expressjs.com/) 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 [ws](https://github.com/websockets/ws) library.\n\n## Installation\n\n`npm install --save express-ws`\n\n## Usage\n\n__Full documentation can be found in the API section below. This section only shows a brief example.__\n\nAdd this line to your Express application:\n\n```javascript\nvar expressWs = require('express-ws')(app);\n```\n\n__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`.\n\nAfter 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).\n\n```javascript\napp.ws('/echo', function(ws, req) {\n  ws.on('message', function(msg) {\n    ws.send(msg);\n  });\n});\n```\n\nIt works with routers, too, this time at `/ws-stuff/echo`:\n\n```javascript\nvar router = express.Router();\n\nrouter.ws('/echo', function(ws, req) {\n  ws.on('message', function(msg) {\n    ws.send(msg);\n  });\n});\n\napp.use(\"/ws-stuff\", router);\n```\n\n## Full example\n\n```javascript\nvar express = require('express');\nvar app = express();\nvar expressWs = require('express-ws')(app);\n\napp.use(function (req, res, next) {\n  console.log('middleware');\n  req.testing = 'testing';\n  return next();\n});\n\napp.get('/', function(req, res, next){\n  console.log('get route', req.testing);\n  res.end();\n});\n\napp.ws('/', function(ws, req) {\n  ws.on('message', function(msg) {\n    console.log(msg);\n  });\n  console.log('socket', req.testing);\n});\n\napp.listen(3000);\n```\n\n## API\n\n### expressWs(app, *server*, *options*)\n\nSets up `express-ws` 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.\n\n* __app__: The Express application to set up `express-ws` on.\n* __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`.\n* __options__: *Optional.* An object containing further options.\n  * __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.\n  * __wsOptions:__ Options object passed to WebSocketServer constructor. Necessary for any ws specific features.\n\nThis function will return a new `express-ws` API object, which will be referred to as `wsInstance` in the rest of the documentation.\n\n### wsInstance.app\n\nThis property contains the `app` that `express-ws` was set up on.\n\n### wsInstance.getWss()\n\nReturns the underlying WebSocket server/handler. You can use `wsInstance.getWss().clients` to obtain a list of all the connected WebSocket clients for this server.\n\nNote 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.\n\n### wsInstance.applyTo(router)\n\nSets up `express-ws` on the given `router` (or other Router-like object). You will only need this in two scenarios:\n\n1. You have enabled `options.leaveRouterUntouched`, or\n2. You are using a custom router that is not based on the express.Router prototype.\n\nIn most cases, you won't need this at all.\n\n## Development\n\nThis module is written in ES6 and uses ESM.\n","readmeFilename":"README.md"}