{"tsd":{"directory":"./test/types"},"bugs":{"url":"https://github.com/fastify/fastify-websocket/issues"},"dist":{"shasum":"5a98014189a53741a29d9c3ae7e2c780cc16f30a","tarball":"https://registry.npmjs.org/fastify-websocket/-/fastify-websocket-2.0.0.tgz","fileCount":12,"integrity":"sha512-VOszJTMMzT4hMBXsxW1GVa+QKVNw3HeJfmc786paQM09aSdxSXVtQ1y7KiLtTgdaVSLVdrlwz1TMg7egjbA0dg==","signatures":[{"sig":"MEUCIEtcBpjT2pPTN6gvsUP63PpEpGacyK/2WltfWimomHPxAiEAsZ8/BqN/fk67L8eqlO8OdcYnAbbJGXMfL4Y8l3lIPSw=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":25359,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJesUMdCRA9TVsSAnZWagAA5x0P/0T6WXGnxp0HUcv0HdG6\nsgAcHrk30ROWPvL0sbrfRlPMR6lsoiDW7cBrHhEHRUFCzfLHz7KVORoYTtc/\nH5mBBdvK6ZQ8b0bnfwGqjRTZCugqv5Y+Stam/on7av/O4mfElp1ojP5Guynx\nBtEBff0AUHIT74gEZ/bR0zbEwxFKonRlvyDK+dWmL3TSWBrppUz1iVYC514P\n/UvlvOpGLKFE4UyXJdJWIeutfvr05O/uc9TQ4RL51fEsiFWnQLBJTXSp0jEs\niP4DMxiUQ7oJK+voWsslYSTVXFmqEzGUUC4DbAa9sWFJDffd5nucIuEpwE56\nPJA8XbcoKx7b8Ydx4P9VgzWVETjjYg6WBy2CdYzAgCPcEFFQ+a6/hBV8hGJK\nYTp0ni0/dfhJNXbmwbd55K/efKpw+nkjHtrB0gO680rtN1Wc427IZF9Iapz6\nMfQsVYGRPv/kkOejCLUjcuxEdvfYfT019JUtA09BfYpYjlLHHcsKyGsacWgw\nbe9fyobJ5WSXQ6mGtIWznzteEL7B4uDfCbPFhGTaQaszrGP/dmTp6EK6SELL\nvEivtkPP9JKYFv5DsqLla22G4QACTXY4CuMhbKMajR6DR4v2dh3+eE+6a3fR\nRX9yvwFaUnR6DKf4R/vzV/u8ePl6b+DC58PKcMcww1Qn6lkVKrfR8lgPbN/r\njQ+7\r\n=oo9i\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":"42a335644572b15e7999d0efad074afdeb4a922e","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.4","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.1","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.0_1588675357517_0.593713944451693","host":"s3://npm-registry-packages"},"_id":"fastify-websocket@2.0.0","version":"2.0.0"}