{"tsd":{"directory":"./test/types"},"bugs":{"url":"https://github.com/fastify/fastify-websocket/issues"},"dist":{"shasum":"db9218c925c393d51836e035d0365cb950f395ed","tarball":"https://registry.npmjs.org/fastify-websocket/-/fastify-websocket-2.0.2.tgz","fileCount":12,"integrity":"sha512-wxEIKXqU3/AC7VdoiDtqZcrfGrw8097gia8VXRunkHtO63X7TADvKEBdA8yBhlREWFXnTfEf65e2RrGUbW5Bzg==","signatures":[{"sig":"MEQCIHBRjWW8VjW3n2I3wpaERl6+XMM8qAUvK4OIXaOtk0DPAiBDDFoiS24mKaJIgrdUpTjkFpIdIHIT1TALcpGTg/5Lzw==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":26166,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe/zswCRA9TVsSAnZWagAACqAQAJC1pvUYSujRRLam1QC9\nfBKLF8aFUADB1JprLm3iB+zcpoqGWE494Tu+NyFh1uR8XjcAhydJHz7lfxce\ndkePpFewpz6vcUKqX0pOIgdbbxWUp3xeP/Na2CHNYyB4l13gt60zq/8/waJF\nEM9qqdGWt7KzsQgurW87JUAQEojxwivpS6q7tBh6nR1vXOlo/X+ZPDdtWjHi\nbjaBkoAyji6L4Sd9RwSLcKdKTp9pkLr2ccVQdItu1k0Grft3izG9wpFplZAH\nSGD+ZUQqcejzzUMNVHpJe3lb71y+Zr9QF7LsUVimiO2kP2XCsYkCq24LahnN\nIcTTLmcTDLxO0xHaXmK5iSyE0V+WhIHBO1+7EW10tkrftyHI/HIfXn4ce2OK\nn10tqGL9my15/8N6RDo8BttrfmE65ohET+/Jy9MH5kD4F4zIdOY0FSGrtL7Y\nLM7jcWwJmyUehZYlk57G9OX8/N4gyPfNvD73Il3wmC6Ce8IlvmroR50yRGzj\nRZcF8EuhqBhqUWBJ2EXtBQuK7MoEsC8R1mFIByhE/ELdCkf05YcGv5/jmnu7\nsxR1GOUGM+PV53eYxof4qOmIvnDQXMbIcnWJ5RDt808LIyco/XfGH6Gauire\noZ1CCyDVFP6OqhFuxuXCBG20+/zLfK0nyZHarQUD3IQzSWEFW1bIZ4p3Jsct\nzSFs\r\n=DNCL\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":"4c954e00a77f783fb056dabe8422aa669fe3ea6d","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.18.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.12.0","snazzy":"^8.0.0","fastify":"^3.0.0-rc.5","standard":"^14.3.3","@types/ws":"^7.2.4","pre-commit":"^1.2.2"},"_npmOperationalInternal":{"tmp":"tmp/fastify-websocket_2.0.2_1593785135684_0.7522504831168224","host":"s3://npm-registry-packages"},"_id":"fastify-websocket@2.0.2","version":"2.0.2"}