{"tsd":{"directory":"./test/types"},"bugs":{"url":"https://github.com/fastify/fastify-websocket/issues"},"dist":{"shasum":"7621f86a294a57bdaf77b572413737c09cb51786","tarball":"https://registry.npmjs.org/fastify-websocket/-/fastify-websocket-2.0.1.tgz","fileCount":12,"integrity":"sha512-QoE+fh4QFGc3NUETqlCXlmseKLcjG6gu0bbxqQQ+q+WVnNS6G50wGCdrwFt7rmxbSjS4j7VDNjQbg71vfounRw==","signatures":[{"sig":"MEQCIDSR76kD6Vu6J8gkzNFfg+S3xMOYqrRM16p/UIK1knNjAiAX0G97FNt95GDIq77BresMikZ+EUuulL6ejt4mzNGryQ==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":26218,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJeznC5CRA9TVsSAnZWagAAqsgP/14bFald4TSEZQbUpFZK\n7w6xpbKTKR7qRvLfYKuIkCM12bBnDAIYGbSTDoo3cHKvZHIwweoD0zZ0hPlY\ncKmMLTpvTjeRGgnJW/iGp005zip34A+pmlqvjoSWfTTbf/xusJg9M0qo/HI8\nCtFN73e+TbB3yib/0pxztl2UDRRwPF8ekNDaix8EqQx2YSQkjTnFFb50nzkK\neMTVMZChXCYgejQRKiEMpSxBmDpmjyz37VkxirKwQmQFCDXVh5s1RHEtMesC\neYOD1qXK8SP3sHcfwyM20VZUtmjSohYWkc0m02K7JnJbnIA3QtsTJNFLt3dX\n3JfrmOz5stNgoeeqPbIbLnKWO/Xnj5aQE3CANep8FAe5h7nB+N/pCrM1u6ew\nrPZLybzU8jn4ECzTRjObzVz0K3uGwRqPxbyjY1NX/u0xdwyir7PIvtpKiTNV\nCRh2f2M3fBrIgqAf1+ZgIhog4HXUKKjMjYTscC+AqUicODMsOMInpB74EJpW\nz5z3mjevwA2mFGAELyMAoKgoG9bjVReaEtBZ7ScOabiYOG4FU/c3p1AtGl5i\np0ZvlfRWybi/8FGYDB8Mv4VNBwh82Lewt3z0wdiWGlJc0tt1eoufmyHA5Typ\n1H+3HtD4OpM4gBhVi69wRDRRVKvE7DpsrcGEaRKGINwzq9c8KjhqtntnRfvb\nn3nm\r\n=RJ+r\r\n-----END PGP SIGNATURE-----\r\n"},"main":"index.js","name":"fastify-websocket","types":"index.d.ts","author":{"name":"Matteo Collina","email":"hello@matteocollina.com"},"readme":"# fastify-websocket\n\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)\n![CI\nworkflow](https://github.com/fastify/fastify-websocket/workflows/CI%20workflow/badge.svg)\n\nWebSocket support for [Fastify](https://github.com/fastify/fastify).\nBuilt upon [ws](https://www.npmjs.com/package/ws).\n\n## Install\n\n```\nnpm install fastify-websocket --save\n```\n\n## Usage\n\nThere are two possible ways of using this plugin: with a **global handler** or with a **per route handler**.\n\n### Global handler\n\nAll you need to do is to add it to your project with `register` and pass handle function. You are done!\n\n```js\n'use strict'\n\nconst fastify = require('fastify')()\n\nfastify.register(require('fastify-websocket'), {\n  handle,\n  options: {\n    maxPayload: 1048576, // we set the maximum allowed messages size to 1 MiB (1024 bytes * 1024 bytes)\n    path: '/fastify', // we accept only connections matching this path e.g.: ws://localhost:3000/fastify\n    verifyClient: function (info, next) {\n      if (info.req.headers['x-fastify-header'] !== 'fastify is awesome !') {\n        return next(false) // the connection is not allowed\n      }\n      next(true) // the connection is allowed\n    }\n  }\n})\n\nfunction handle (conn) {\n  conn.pipe(conn) // creates an echo server\n}\n\nfastify.listen(3000, err => {\n  if (err) {\n    fastify.log.error(err)\n    process.exit(1)\n  }\n})\n```\n\n### Per route handler\n\nAfter registering this plugin, you can choose on which routes the WS server will respond. This could be achieved by adding `websocket: true` property to `routeOptions` on a fastify's `.get` route. In this case two arguments will be passed to the handler: socket connection and the original `http.IncomingMessage` (instead of the usual fastify's request and reply objects).\n\n```js\n'use strict'\n\nconst fastify = Fastify()\n\nfastify.register(require('fastify-websocket'))\n\nfastify.get('/', { websocket: true }, (connection, req) => {\n  connection.socket.on('message', message => {\n    // message === 'hi from client'\n    connection.socket.send('hi from server')\n  })\n})\n\nfastify.listen(3000, err => {\n  if (err) {\n    fastify.log.error(err)\n    process.exit(1)\n  }\n})\n```\n\nIn this case there won't be any global handler, so it will respond with a 404 error on every unregistered route, closing the incoming upgrade connection requests.\n\nThe route handler receives route params as a third argument:\n\n```js\nfastify.get('/ws/:id', { websocket: true }, (connection, req, params) => {\n  connection.write(`hi on stream ${params.id}`)\n})\n```\n\nHowever you can still pass a default global handler, that will be used as default handler.\n\n```js\n'use strict'\n\nconst fastify = require('fastify')()\n\nfunction handle (conn) {\n  conn.pipe(conn) // creates an echo server\n}\n\nfastify.register(require('fastify-websocket'), {\n  handle,\n  options: { maxPayload: 1048576 }\n})\n\nfastify.get('/', { websocket: true }, (connection, req) => {\n  connection.socket.on('message', message => {\n    // message === 'hi from client'\n    connection.socket.send('hi from server')\n  })\n})\n\nfastify.listen(3000, err => {\n  if (err) {\n    fastify.log.error(err)\n    process.exit(1)\n  }\n})\n```\n\nIf you need to handle both HTTP requests and incoming socket connections on the same route, you can still do it using the [full declaration syntax](https://www.fastify.io/docs/latest/Routes/#full-declaration), adding a `wsHandler` property.\n\n```js\n'use strict'\n\nconst fastify = require('fastify')()\n\nfunction handle (conn) {\n  conn.pipe(conn) // creates an echo server\n}\n\nfastify.register(require('fastify-websocket'), {\n  handle,\n  options: { maxPayload: 1048576 }\n})\n\nfastify.route({\n  method: 'GET',\n  url: '/hello',\n  handler: (req, reply) => {\n    // this will handle http requests\n    reply.send({ hello: 'world' })\n  },\n  wsHandler: (conn, req) => {\n    // this will handle websockets connections\n    conn.setEncoding('utf8')\n    conn.write('hello client')\n\n    conn.once('data', chunk => {\n      conn.end()\n    })\n  }\n})\n\nfastify.listen(3000, err => {\n  if (err) {\n    fastify.log.error(err)\n    process.exit(1)\n  }\n})\n```\n\n## Options :\n\n`fastify-websocket` accept the same options as [`ws`](https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback) :\n\n- `objectMode` - Send each chunk on its own, and do not try to pack them in a single websocket frame.\n- `host` - The hostname where to bind the server.\n- `port` - The port where to bind the server.\n- `backlog` - The maximum length of the queue of pending connections.\n- `server` - A pre-created Node.js HTTP/S server.\n- `verifyClient` - A function which can be used to validate incoming connections.\n- `handleProtocols` - A function which can be used to handle the WebSocket subprotocols.\n- `path` - Accept only connections matching this path.\n- `noServer` - Enable no server mode.\n- `clientTracking` - Specifies whether or not to track clients.\n- `perMessageDeflate` - Enable/disable permessage-deflate.\n- `maxPayload` - The maximum allowed message size in bytes.\n\nFor more informations you can check [`ws` options documentation](https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback).\n\n_**NB:** By default if you do not provide a `server` option `fastify-websocket` will bind your websocket server instance to the scoped `fastify` instance._\n\n## Acknowledgements\n\nThis project is kindly sponsored by [nearForm](http://nearform.com).\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","gitHead":"7bc32fa07b6294515ebba474dd24c5d1edade5dd","license":"MIT","scripts":{"lint":"standard | snazzy","test":"npm run lint && npm run test:js && npm run test:ts","unit":"tap \"test/*.js\"","test:js":"npm run lint && npm run unit","test:ts":"tsd"},"_npmUser":{"name":"matteo.collina","email":"hello@matteocollina.com"},"homepage":"https://github.com/fastify/fastify-websocket#readme","keywords":["fastify","websocket"],"repository":{"url":"git+https://github.com/fastify/fastify-websocket.git","type":"git"},"_npmVersion":"6.14.5","description":"basic websocket support for fastify","directories":{},"maintainers":[{"name":"allevo","email":"tomallevi@gmail.com"},{"name":"cemremengu","email":"cemremengu@gmail.com"},{"name":"delvedor","email":"tommydelved@gmail.com"},{"name":"eomm","email":"behemoth89@gmail.com"},{"name":"ethan_arrowood","email":"ethan.arrowood@gmail.com"},{"name":"jsumners","email":"james.sumners@gmail.com"},{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"starptech","email":"deusdustin@gmail.com"}],"_nodeVersion":"12.16.3","dependencies":{"ws":"^7.2.3","find-my-way":"^3.0.0","fastify-plugin":"^2.0.0"},"_hasShrinkwrap":false,"readmeFilename":"README.md","devDependencies":{"tap":"^14.10.7","tsd":"^0.11.0","snazzy":"^8.0.0","fastify":"^3.0.0-rc.1","standard":"^14.3.3","@types/ws":"^7.2.4","pre-commit":"^1.2.2"},"_npmOperationalInternal":{"tmp":"tmp/fastify-websocket_2.0.1_1590587576549_0.24110153193237172","host":"s3://npm-registry-packages"},"_id":"fastify-websocket@2.0.1","version":"2.0.1"}