{"id":23510852,"url":"https://github.com/danielkov/wsocket.io","last_synced_at":"2025-08-10T06:43:08.652Z","repository":{"id":57400095,"uuid":"74214375","full_name":"danielkov/wsocket.io","owner":"danielkov","description":"ES6 ws client and server wrapper for ease of use.","archived":false,"fork":false,"pushed_at":"2016-12-08T11:30:44.000Z","size":60,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-10T06:43:07.997Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danielkov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-19T14:44:31.000Z","updated_at":"2016-11-19T15:58:40.000Z","dependencies_parsed_at":"2022-09-19T01:10:25.848Z","dependency_job_id":null,"html_url":"https://github.com/danielkov/wsocket.io","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/danielkov/wsocket.io","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Fwsocket.io","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Fwsocket.io/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Fwsocket.io/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Fwsocket.io/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielkov","download_url":"https://codeload.github.com/danielkov/wsocket.io/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Fwsocket.io/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269688004,"owners_count":24459396,"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","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-12-25T12:12:35.347Z","updated_at":"2025-08-10T06:43:08.594Z","avatar_url":"https://github.com/danielkov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WSocket.io\n\nSimple ws wrapper written with ES6 syntax for ease of use.\n\n### Notice\n\nThis is the repo for the NPM module that pulls in both [wsocket.io-client](https://github.com/danielkov/wsocket.io-client) and [wsocket.io-server](https://github.com/danielkov/wsocket.io-server) modules. If you notice any errors, mistakes or just want to share your thoughts and contribute to this project, feel free to send pull requests on the GitHub pages of the respective projects.\n\n### Simple use\n\nserver.js\n```js\nconst SocketServer = require('wsocket.io').Server;\n\n/*\nDefault port is 8080\n*/\nlet wss = new SocketServer()\n\nwss.connect((ws) =\u003e {\n  ws.send('welcome', {message:'hello world'});\n  ws.on('reply', (data) =\u003e {\n    console.log(data);\n  })\n})\n```\n\nclient.js\n```js\nimport Client from wsocket.io;\n\n/*\nDefault port is 8080\n*/\nlet client = new Client();\n\nclient.on('welcome', (data) =\u003e {\n  console.log(data);\n  client.send('reply', {message: 'hello back'});\n})\n\nclient.off();\n```\n\n## Supported methods\n\n### Client\n\n### `.constructor([url: String \u003coptional\u003e])`\nThe parameter is the url to open a WebSocket connection with. This defaults to `window.location.hostname` on port: 8080.\n\n### `.on([name: String], [fn: function \u003ccallback\u003e])`\nHandles incoming messages matching the name provided in the first parameter.\n\n### `.send([name: String], [data: Object])`\nSends a message to the server with the name of first parameter and the data in the form of a stringified object.\n\n### `.off([fn: function \u003ccallback, optional\u003e])`\nCloses open WebSocket connection if there is one and executes callback. If there aren't any open connections the callback will receive an error. If no callback is provided an Error will be thrown.\n\n\n### Server\n\n### `.constructor([opts: Object])`\nSends the `opts` object into the original ws Server constructor. Defaults to `{port: 8080}`.\n\n### `.connect([fn: function \u003ccallback\u003e])`\nThe callback handles each separate incoming connection. It receives a single Socket object (read below).\n\n### `.send([name: String], [data: Object])`\nSends the data in the form of a stringified object to each open connection.\n\n### `.sendTo([id: String], [name: String], [data: Object])`\nSends the data to the socket with the id provided in the first parameter.\n\n### `.sendExclude([id: String], [name: String], [data: Object])`\nSends the data to all the sockets excluding the one with the id in the first parameter. This is the underlying method of `Socket.broadcast()`.\n\n### `.close()`\nCloses the server.\n\n\n### Socket\n\n### `.constructor([ws: WebSocket], [id: String], [handler: Object \u003cServer\u003e])`\nCreates a new Socket object and assigns an id, which is stored in `Server._sockets` object by id.\n\n### `.on([name: String], [fn: function \u003ccallback\u003e])`\nHandles incoming messages matching the name provided in the first parameter.\n\n### `.send([name: String], [data: Object])`\nSends a message to the socket in the form of a stringified object.\n\n### `.broadcast()`\nSends a message to all sockets but the current one, using the server's `sendExclude()` method.\n\n### `.off()`\nRemoves socket from the handled sockets. Triggered automatically when socket closes on the client side.\n\n\n## Changes in Version 1.0\n\n### Event middleware\n\nThe middleware-based approach is quite popular among JavaScript developers, especially on the server-side, which is why I've decided to add support for multiple handle functions for the same WebSocket event.\n\nMultiple handlers on the same event can work in various ways:\n\nVia multiple functions:\n\n```js\nws.on('example',\ndata =\u003e {\n  console.log(data);\n},\ndata =\u003e {\n  storeLogs(data);\n},\ndata =\u003e {\n  ws.send('response', { message: `Your message: ${data.message}, has been stored.`});\n})\n```\n\nBy passing in an array of functions:\n\n```js\nws.on('example',\n[\n  data =\u003e {\n    console.log(data);\n  },\n  data =\u003e {\n    storeLogs(data);\n  },\n  data =\u003e {\n    ws.send('response', { message: `Your message: ${data.message}, has been stored.`});\n  }\n])\n```\n\nAside from the previous 2 best practices, it won't break if you do something like this:\n\n```js\nws.on('example', [\n  data =\u003e {\n    console.log('Hi there!');\n  },\n  data =\u003e {\n    console.log('This will also work.');\n  }\n],\ndata =\u003e {\n  console.log(`Someone sent me: ${data.message}`);\n},\ndata =\u003e {\n  ws.send('reply', { message: 'This is cool.' })\n}\n)\n```\n\n### Support for `on('close')`\n\nThis had been an oversight by me, as it required some tweaking, but the method on server-side now works and is called before the socket finally closes. The parameter of the callback will receive an object `{ id: clientId }` so that identification of the disconnected user is easy.\n\n### Subscribe to multiple WebSocket events with same function\n\nThis change also supports the use of middleware. You can now subscribe to multiple events, using this API. You can separate the events you want to assign the function to, separated with spaces.\n\nExample:\n\n```js\nws.on('message reply login logout', data =\u003e {\n  console.log(`Oh look, we got some data: ${data.message}!`);\n})\n/*send('message') will trigger this once, and so will send('reply') and so on...*/\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielkov%2Fwsocket.io","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielkov%2Fwsocket.io","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielkov%2Fwsocket.io/lists"}