{"id":13515491,"url":"https://github.com/hugmanrique/turbo-ws","last_synced_at":"2025-03-31T04:37:06.837Z","repository":{"id":57116754,"uuid":"124703848","full_name":"hugmanrique/turbo-ws","owner":"hugmanrique","description":":dash: Blazing fast low-level WebSocket server","archived":true,"fork":false,"pushed_at":"2018-04-20T21:12:28.000Z","size":695,"stargazers_count":640,"open_issues_count":2,"forks_count":23,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-03-07T19:17:22.757Z","etag":null,"topics":["nodejs","turbo-net","websocket","websocket-server"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hugmanrique.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-10T22:30:03.000Z","updated_at":"2025-02-28T03:02:19.000Z","dependencies_parsed_at":"2022-08-22T22:20:15.707Z","dependency_job_id":null,"html_url":"https://github.com/hugmanrique/turbo-ws","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hugmanrique%2Fturbo-ws","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hugmanrique%2Fturbo-ws/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hugmanrique%2Fturbo-ws/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hugmanrique%2Fturbo-ws/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hugmanrique","download_url":"https://codeload.github.com/hugmanrique/turbo-ws/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246418657,"owners_count":20773934,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nodejs","turbo-net","websocket","websocket-server"],"created_at":"2024-08-01T05:01:11.949Z","updated_at":"2025-03-31T04:37:05.595Z","avatar_url":"https://github.com/hugmanrique.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# :dash: turbo-ws\n\n[![npm][npm]][npm-url]\n[![node][node]][node-url]\n[![deps][deps]][deps-url]\n[![tests][tests]][tests-url]\n[![coverage][cover]][cover-url]\n[![license][license]][license-url]\n\nA blazing fast low-level WebSocket server based on [turbo-net](https://github.com/mafintosh/turbo-net) and the awesome [ws](https://github.com/websockets/ws) server library for Node.js\n\n## Features\n\n* Supports thousands of concurrent connections with minimal CPU and RAM impact.\n* Binary and text frames are supported. That means you can directly send Node's `Buffer`s or `String`s if you prefer.\n* Built with reliability in mind.\n\n## Getting Started\n\nInstall turbo-ws using [`npm`](https://www.npmjs.com/):\n\n```bash\nnpm install --save @hugmanrique/turbo-ws\n```\n\nOr via [`yarn`](https://yarnpkg.com/en/package/@hugmanrique/turbo-ws):\n\n```bash\nyarn add @hugmanrique/turbo-ws\n```\n\nThe minimum supported Node version is `v8.10.0`.\n\nLet's get started by creating a WebSocket server:\n\n```javascript\nimport Server from '@hugmanrique/turbo-ws';\n\nconst server = new Server();\nconst port = 80;\n```\n\nThen, add a `'connection'` listener. The callback will contain a [`Connection`](https://github.com/mafintosh/turbo-net#connectiononconnect) and a [`Request`](https://github.com/mafintosh/turbo-http#requrl) object:\n\n```javascript\nserver.on('connection', (connection, req) =\u003e {\n  const userAgent = req.getHeader('User-Agent');\n\n  console.log(`Using ${userAgent} browser`);\n  connection.send('Hello world!');\n});\n```\n\nFinally call the `#listen(port)` method and run your Node app:\n\n```javascript\nserver.listen(port).then(() =\u003e {\n  console.log(`Listening on *:${port}`);\n});\n```\n\n## Methods\n\n#### `connection.send(data)`\n\nSends data to the client. Depending on the type of the passed object, it will send the data in one or multiple frames:\n\n* Strings get sent directly in one frame.\n* Objects get converted to strings through `JSON.stringify`.\n* Node's [Buffers](https://nodejs.org/api/buffer.html) get sent as binary data and may be sent in multiple frames.\n\n#### `connection.ping([payload])`\n\nSends a ping frame that may contain a payload. The client must send a Pong frame with the same payload in response. Check the [`connection.on('pong')`](#connectiononpong) method for more details.\n\n#### `connection.close([callback])`\n\nCloses the connection.\n\n#### `server.broadcast(data)`\n\nSends data to all the clients. Follows the same logic as [`connection.send(data)`](#connectionsenddata)\n\n#### `server.getConnections()`\n\nGet an unordered array containing the current active [connections](https://github.com/mafintosh/turbo-net#connectiononconnect).\n\n#### `server.close()`\n\nCloses the server. Returns a `Promise` that will get completed when all the connections are closed.\n\n## Events\n\nBoth the `Server` and the `Connection` are [EventEmitters](https://nodejs.org/api/events.html#events_class_eventemitter), so you can listen to these events:\n\n#### `server.on('connection', connection)`\n\nEmitted when a new connection is established. The callback arguments are `connection, request`, where the first is a turbo-net [Connection](https://github.com/mafintosh/turbo-net#connectiononconnect) and the later is a turbo-http [Request](https://github.com/mafintosh/turbo-http#requrl) object. Check out their docs to see what fields and methods are available.\n\n#### `server.on('close')`\n\nEmitted when the server has terminated all the WebSocket connections and it is going to die.\n\n#### `connection.on('text', string)`\n\nEmitted when the client sends some text data.\n\n#### `connection.on('binary', binaryStream)`\n\nEmitted when the client sends some buffered binary data. Returns a [BinaryStream](src/binaryStream.js), which is a wrapper for Node's [`stream.Readable`](https://nodejs.org/api/stream.html#stream_readable_streams). You can then add listeners to the stream:\n\n```javascript\nconst writable = fs.createWriteStream('file.txt');\n\nconnection.on('binary', stream =\u003e {\n  stream.pipe(writable);\n\n  stream.on('end', () =\u003e {\n    console.log('Saved client logs to disk!');\n  });\n});\n```\n\n#### `connection.on('ping')`\n\nEmitted when the server received a [Ping frame](https://tools.ietf.org/html/rfc6455#section-5.5.2) from the client.\n\n#### `connection.on('pong')`\n\nEmitted when the server received a [Pong frame](https://tools.ietf.org/html/rfc6455#section-5.5.3) from the client.\n\n#### `connection.on('close')`\n\nEmitted when a connection is fully closed. No other events will be emitted after.\n\n# License\n\n[MIT](LICENSE) \u0026copy; [Hugo Manrique](https://hugmanrique.me)\n\n[npm]: https://img.shields.io/npm/v/@hugmanrique/turbo-ws.svg\n[npm-url]: https://npmjs.com/package/@hugmanrique/turbo-ws\n[node]: https://img.shields.io/node/v/@hugmanrique/turbo-ws.svg\n[node-url]: https://nodejs.org\n[deps]: https://img.shields.io/david/hugmanrique/turbo-ws.svg\n[deps-url]: https://david-dm.org/hugmanrique/turbo-ws\n[tests]: https://img.shields.io/travis/hugmanrique/turbo-ws/master.svg\n[tests-url]: https://travis-ci.org/hugmanrique/turbo-ws\n[license-url]: LICENSE\n[license]: https://img.shields.io/github/license/hugmanrique/turbo-ws.svg\n[cover]: https://img.shields.io/coveralls/hugmanrique/turbo-ws.svg\n[cover-url]: https://coveralls.io/r/hugmanrique/turbo-ws/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhugmanrique%2Fturbo-ws","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhugmanrique%2Fturbo-ws","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhugmanrique%2Fturbo-ws/lists"}