{ "_args": [ [ { "raw": "ws@~2.3.1", "scope": null, "escapedName": "ws", "name": "ws", "rawSpec": "~2.3.1", "spec": ">=2.3.1 <2.4.0", "type": "range" }, "/Users/fzy/project/3mang/node_modules/engine.io" ] ], "_from": "ws@>=2.3.1 <2.4.0", "_id": "ws@2.3.1", "_inCache": true, "_location": "/ws", "_nodeVersion": "7.9.0", "_npmOperationalInternal": { "host": "packages-12-west.internal.npmjs.com", "tmp": "tmp/ws-2.3.1.tgz_1492711201097_0.04034068179316819" }, "_npmUser": { "name": "lpinca", "email": "luigipinca@gmail.com" }, "_npmVersion": "4.2.0", "_phantomChildren": {}, "_requested": { "raw": "ws@~2.3.1", "scope": null, "escapedName": "ws", "name": "ws", "rawSpec": "~2.3.1", "spec": ">=2.3.1 <2.4.0", "type": "range" }, "_requiredBy": [ "/engine.io", "/engine.io-client" ], "_resolved": "https://registry.npmjs.org/ws/-/ws-2.3.1.tgz", "_shasum": "6b94b3e447cb6a363f785eaf94af6359e8e81c80", "_shrinkwrap": null, "_spec": "ws@~2.3.1", "_where": "/Users/fzy/project/3mang/node_modules/engine.io", "author": { "name": "Einar Otto Stangvik", "email": "einaros@gmail.com", "url": "http://2x.io" }, "bugs": { "url": "https://github.com/websockets/ws/issues" }, "dependencies": { "safe-buffer": "~5.0.1", "ultron": "~1.1.0" }, "description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js", "devDependencies": { "benchmark": "~2.1.2", "bufferutil": "~3.0.0", "eslint": "~3.19.0", "eslint-config-standard": "~10.2.0", "eslint-plugin-import": "~2.2.0", "eslint-plugin-node": "~4.2.0", "eslint-plugin-promise": "~3.5.0", "eslint-plugin-standard": "~3.0.0", "mocha": "~3.2.0", "nyc": "~10.2.0", "utf-8-validate": "~3.0.0" }, "directories": {}, "dist": { "shasum": "6b94b3e447cb6a363f785eaf94af6359e8e81c80", "tarball": "https://registry.npmjs.org/ws/-/ws-2.3.1.tgz" }, "files": [ "index.js", "lib" ], "gitHead": "732aaf06b76700f104eeff2740e1896be4e88199", "homepage": "https://github.com/websockets/ws", "keywords": [ "HyBi", "Push", "RFC-6455", "WebSocket", "WebSockets", "real-time" ], "license": "MIT", "main": "index.js", "maintainers": [ { "name": "3rdeden", "email": "npm@3rd-Eden.com" }, { "name": "einaros", "email": "einaros@gmail.com" }, { "name": "lpinca", "email": "luigipinca@gmail.com" }, { "name": "v1", "email": "npm@3rd-Eden.com" } ], "name": "ws", "optionalDependencies": {}, "readme": "# ws: a Node.js WebSocket library\n\n[](https://www.npmjs.com/package/ws)\n[](https://travis-ci.org/websockets/ws)\n[](https://ci.appveyor.com/project/lpinca/ws)\n[](https://coveralls.io/r/websockets/ws?branch=master)\n\n`ws` is a simple to use, blazing fast, and thoroughly tested WebSocket client\nand server implementation.\n\nPasses the quite extensive Autobahn test suite. See http://websockets.github.io/ws/\nfor the full reports.\n\n**Note**: This module does not work in the browser. The client in the docs is a\nreference to a back end with the role of a client in the WebSocket\ncommunication. Browser clients must use the native\n[`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) object.\n\n## Protocol support\n\n* **HyBi drafts 07-12** (Use the option `protocolVersion: 8`)\n* **HyBi drafts 13-17** (Current default, alternatively option `protocolVersion: 13`)\n\n## Installing\n\n```\nnpm install --save ws\n```\n\n### Opt-in for performance and spec compliance\n\nThere are 2 optional modules that can be installed along side with the `ws`\nmodule. These modules are binary addons which improve certain operations.\nPrebuilt binaries are available for the most popular platforms so you don't\nnecessarily need to have a C++ compiler installed on your machine.\n\n- `npm install --save-optional bufferutil`: Allows to efficiently perform\n operations such as masking and unmasking the data payload of the WebSocket\n frames.\n- `npm install --save-optional utf-8-validate`: Allows to efficiently check\n if a message contains valid UTF-8 as required by the spec.\n\n## API Docs\n\nSee [`/doc/ws.md`](https://github.com/websockets/ws/blob/master/doc/ws.md)\nfor Node.js-like docs for the ws classes.\n\n## WebSocket compression\n\n`ws` supports the [permessage-deflate extension][permessage-deflate] which\nenables the client and server to negotiate a compression algorithm and its\nparameters, and then selectively apply it to the data payloads of each\nWebSocket message.\n\nThe extension is enabled by default but adds a significant overhead in terms of\nperformance and memory comsumption. We suggest to use WebSocket compression\nonly if it is really needed.\n\nTo disable the extension you can set the `perMessageDeflate` option to `false`.\nOn the server:\n\n```js\nconst WebSocket = require('ws');\n\nconst wss = new WebSocket.Server({\n perMessageDeflate: false,\n port: 8080\n});\n```\n\nOn the client:\n\n```js\nconst WebSocket = require('ws');\n\nconst ws = new WebSocket('ws://www.host.com/path', {\n perMessageDeflate: false\n});\n```\n\n## Usage examples\n\n### Sending and receiving text data\n\n```js\nconst WebSocket = require('ws');\n\nconst ws = new WebSocket('ws://www.host.com/path');\n\nws.on('open', function open() {\n ws.send('something');\n});\n\nws.on('message', function incoming(data, flags) {\n // flags.binary will be set if a binary data is received.\n // flags.masked will be set if the data was masked.\n});\n```\n\n### Sending binary data\n\n```js\nconst WebSocket = require('ws');\n\nconst ws = new WebSocket('ws://www.host.com/path');\n\nws.on('open', function open() {\n const array = new Float32Array(5);\n\n for (var i = 0; i < array.length; ++i) {\n array[i] = i / 2;\n }\n\n ws.send(array);\n});\n```\n\n### Server example\n\n```js\nconst WebSocket = require('ws');\n\nconst wss = new WebSocket.Server({ port: 8080 });\n\nwss.on('connection', function connection(ws) {\n ws.on('message', function incoming(message) {\n console.log('received: %s', message);\n });\n\n ws.send('something');\n});\n```\n\n### Broadcast example\n\n```js\nconst WebSocket = require('ws');\n\nconst wss = new WebSocket.Server({ port: 8080 });\n\n// Broadcast to all.\nwss.broadcast = function broadcast(data) {\n wss.clients.forEach(function each(client) {\n if (client.readyState === WebSocket.OPEN) {\n client.send(data);\n }\n });\n};\n\nwss.on('connection', function connection(ws) {\n ws.on('message', function incoming(data) {\n // Broadcast to everyone else.\n wss.clients.forEach(function each(client) {\n if (client !== ws && client.readyState === WebSocket.OPEN) {\n client.send(data);\n }\n });\n });\n});\n```\n\n### ExpressJS example\n\n```js\nconst express = require('express');\nconst http = require('http');\nconst url = require('url');\nconst WebSocket = require('ws');\n\nconst app = express();\n\napp.use(function (req, res) {\n res.send({ msg: \"hello\" });\n});\n\nconst server = http.createServer(app);\nconst wss = new WebSocket.Server({ server });\n\nwss.on('connection', function connection(ws) {\n const location = url.parse(ws.upgradeReq.url, true);\n // You might use location.query.access_token to authenticate or share sessions\n // or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312)\n\n ws.on('message', function incoming(message) {\n console.log('received: %s', message);\n });\n\n ws.send('something');\n});\n\nserver.listen(8080, function listening() {\n console.log('Listening on %d', server.address().port);\n});\n```\n\n### echo.websocket.org demo\n\n```js\nconst WebSocket = require('ws');\n\nconst ws = new WebSocket('wss://echo.websocket.org/', {\n origin: 'https://websocket.org'\n});\n\nws.on('open', function open() {\n console.log('connected');\n ws.send(Date.now());\n});\n\nws.on('close', function close() {\n console.log('disconnected');\n});\n\nws.on('message', function incoming(data, flags) {\n console.log(`Roundtrip time: ${Date.now() - data} ms`, flags);\n\n setTimeout(function timeout() {\n ws.send(Date.now());\n }, 500);\n});\n```\n\n### Other examples\n\nFor a full example with a browser client communicating with a ws server, see the\nexamples folder.\n\nOtherwise, see the test cases.\n\n## Error handling best practices\n\n```js\n// If the WebSocket is closed before the following send is attempted\nws.send('something');\n\n// Errors (both immediate and async write errors) can be detected in an optional\n// callback. The callback is also the only way of being notified that data has\n// actually been sent.\nws.send('something', function ack(error) {\n // If error is not defined, the send has been completed, otherwise the error\n // object will indicate what failed.\n});\n\n// Immediate errors can also be handled with `try...catch`, but **note** that\n// since sends are inherently asynchronous, socket write failures will *not* be\n// captured when this technique is used.\ntry { ws.send('something'); }\ncatch (e) { /* handle error */ }\n```\n\n## Changelog\n\nWe're using the GitHub [`releases`](https://github.com/websockets/ws/releases)\nfor changelog entries.\n\n## License\n\n[MIT](LICENSE)\n\n[permessage-deflate]: https://tools.ietf.org/html/rfc7692\n", "readmeFilename": "README.md", "repository": { "type": "git", "url": "git+https://github.com/websockets/ws.git" }, "scripts": { "integration": "eslint . && mocha test/*.integration.js", "lint": "eslint .", "test": "eslint . && nyc --reporter=html --reporter=text mocha test/*.test.js" }, "version": "2.3.1" }