{"id":26414930,"url":"https://github.com/gabeduartem/simple-ws-wrapper","last_synced_at":"2026-04-26T20:31:45.288Z","repository":{"id":52542930,"uuid":"169883085","full_name":"GabeDuarteM/simple-ws-wrapper","owner":"GabeDuarteM","description":"Provides a socket.io inspired API for sockets, but using native websockets protocol","archived":false,"fork":false,"pushed_at":"2022-11-20T19:30:25.000Z","size":141,"stargazers_count":2,"open_issues_count":14,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-11T01:22:51.143Z","etag":null,"topics":["native","socket","socket-io","socketio","sockets","web","websocket","websockets","ws"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"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/GabeDuarteM.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-09T15:51:54.000Z","updated_at":"2023-03-10T08:36:22.000Z","dependencies_parsed_at":"2023-01-23T14:00:36.644Z","dependency_job_id":null,"html_url":"https://github.com/GabeDuarteM/simple-ws-wrapper","commit_stats":null,"previous_names":["gabrielduartem/simple-ws-wrapper"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/GabeDuarteM/simple-ws-wrapper","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GabeDuarteM%2Fsimple-ws-wrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GabeDuarteM%2Fsimple-ws-wrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GabeDuarteM%2Fsimple-ws-wrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GabeDuarteM%2Fsimple-ws-wrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GabeDuarteM","download_url":"https://codeload.github.com/GabeDuarteM/simple-ws-wrapper/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GabeDuarteM%2Fsimple-ws-wrapper/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32312223,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T19:15:34.056Z","status":"ssl_error","status_checked_at":"2026-04-26T19:15:15.467Z","response_time":129,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["native","socket","socket-io","socketio","sockets","web","websocket","websockets","ws"],"created_at":"2025-03-18T00:16:47.161Z","updated_at":"2026-04-26T20:31:45.271Z","avatar_url":"https://github.com/GabeDuarteM.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003esimple-ws-wrapper\u003c/h1\u003e\n\n  \u003cp\u003eProvides a socket.io inspired API for sockets, but using native websockets protocol.\u003c/p\u003e\n\u003c/div\u003e\n\n\u003chr /\u003e\n\n[![Build Status][build-badge]][build]\n[![version][version-badge]][package]\n[![downloads][downloads-badge]][npmtrends]\n[![MIT License][license-badge]][license]\n\n[![PRs Welcome][prs-badge]][prs]\n[![Code of Conduct][coc-badge]][coc]\n\n[![Watch on GitHub][github-watch-badge]][github-watch]\n[![Star on GitHub][github-star-badge]][github-star]\n[![Tweet][twitter-badge]][twitter]\n\nThis lib is isomorphic, so it works on both `node` (using [`ws`](https://www.npmjs.com/package/ws)) and on browsers (using the native [`WebSocket` class](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API)).\n\n## Installation\n\nThis module is distributed via [npm][npm] which is bundled with [node][node] and\nshould be installed as one of your project's `dependencies`:\n\n```bash\nnpm install --save simple-ws-wrapper\n```\n\n## The problem\n\nThere are a lot of libraries that provide a great API for interacting via WebSockets, but most of them require a custom client to use it. Because of this, it becomes harder to use them on a cross-platform environment (e.g. the server made using `node` and the client being a native mobile app). Because they do not work with native WebSocket clients, if the library does not provide the client for accessing it on the environment you are using, you just cannot use it.\n\nAs an alternative we could use libraries like `ws`, which does not require any client package to interact with the server, but unfortunately it does not provide the best of the API's for that, so you end up with a lot of weird boilerplate just to manage the messages.\n\n## This solution\n\nWhen using on a `node` environment, this is just a simple wrapper around `ws`, and when on browsers, it is a wrapper around the native `WebSocket` class. In both cases, we provide the same `socket.io` inspired API for exchanging messages between server and client.\n\nBecause `ws` works natively with native websocket clients, if you create a server using this package, you can use it in whatever enviromnent which supports `websockets` without any additional package.\n\nUnder the hood, we work with objects with the shape below, so if you don't plan to use our wrapper on the client as well, you can just send an object with that shape and everything will work the same.\n\n```ts\n{\n  type: string;\n  data?: any;\n}\n```\n\n## Usage\n\n### Server\n\n```js\nconst ws = require('ws')\nconst SocketWrapper = require('simple-ws-wrapper')\n\nconst socket = new SocketWrapper(new ws.Server({ port: 3000 }))\n\nconst messages = []\n\nsocket.on('connection', () =\u003e {\n  socket.emit(\n    'welcome',\n    'Welcome! You have successfully connected to the server',\n  )\n})\n\nsocket.on('request-initial-data', () =\u003e {\n  socket.emit('initial-data', { name: 'James', surename: 'Bond' })\n})\n\nsocket.on('add-message', (message) =\u003e {\n  messages.push(message)\n  console.log(messages) // [{ date: \"2019-02-09T13:53:52.058Z\", message: \"hey there!\" }]\n})\n```\n\n### Client\n\n```js\nconst WebSocket = require('ws')\nconst SocketWrapper = require('simple-ws-wrapper')\n\nconst socket = new SocketWrapper(new WebSocket('ws://localhost:3000'))\n\nsocket.on('welcome', (data) =\u003e {\n  console.log(data) // \"Welcome! You have successfully connected to the server\"\n})\n\nsocket.on('initial-data', (data) =\u003e {\n  console.log(data) // { name: \"James\", surename: \"Bond\" }\n})\n\nconst onConnect = async () =\u003e {\n  await socket.waitConnection()\n  socket.emit('request-initial-data')\n  socket.emit('add-message', {\n    date: '2019-02-09T13:53:52.058Z',\n    message: 'hey there!',\n  })\n}\n\nonConnect()\n```\n\n### Browser\n\nBrowser usage is essentially the same as described in `Client` above, the only difference is that you use the native [`WebSocket` class](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API), instead of the [`ws`](https://www.npmjs.com/package/ws) package.\n\n### API\n\n#### `.emit(type: string, data?: any): void`\n\nEmits a message of given `type` to all listeners.\n\n`type`: Type of the message which will be sent.\n\n`data`: Optional. Data of the message.\n\n```js\nsocket.emit('request-initial-data')\nsocket.emit('add-message', {\n  date: '2019-02-09T13:53:52.058Z',\n  message: 'hey there!',\n})\n```\n\n#### `.on(type: string, handler: (data: any) =\u003e void): void`\n\nAdds a listener on given `type` message.\n\n`type`: Which message will be listened.\n\n`handler`: Function to be executed when given `type` is called. The first argument of the function is the data of the message.\n\n```js\nsocket.on('add-message', (data) =\u003e {\n  console.log(data) // { date: \"2019-02-09T13:53:52.058Z\", message: \"hey there!\" }\n})\n```\n\n#### `.waitConnection(): Promise\u003cvoid\u003e`\n\nOn a server, will resolve when a client has connected.\n\nOn a client, will resolve when it connects to the server.\n\n## Inspiration\n\nThe API is heavily inspired by the awesome [`socket.io`](https://socket.io).\n\n## Other Solutions\n\n- [`ws`](https://www.npmjs.com/package/ws): The library which we use internally to provide the wrapper.\n\nI'm not aware of any others, if you are please [make a pull request][prs] and add it\nhere!\n\n## LICENSE\n\nMIT\n\n[npm]: https://www.npmjs.com/\n[node]: https://nodejs.org\n[build-badge]: https://img.shields.io/travis/com/GabrielDuarteM/simple-ws-wrapper/master.svg?style=flat-square\n[build]: https://travis-ci.com/GabrielDuarteM/simple-ws-wrapper\n[coverage-badge]: https://img.shields.io/codecov/c/github/GabrielDuarteM/simple-ws-wrapper.svg?style=flat-square\n[coverage]: https://codecov.io/github/GabrielDuarteM/simple-ws-wrapper\n[version-badge]: https://img.shields.io/npm/v/simple-ws-wrapper.svg?style=flat-square\n[package]: https://www.npmjs.com/package/simple-ws-wrapper\n[downloads-badge]: https://img.shields.io/npm/dm/simple-ws-wrapper.svg?style=flat-square\n[npmtrends]: http://www.npmtrends.com/simple-ws-wrapper\n[license-badge]: https://img.shields.io/github/license/GabrielDuarteM/simple-ws-wrapper.svg?style=flat-square\n[license]: https://github.com/GabrielDuarteM/simple-ws-wrapper/blob/master/LICENSE\n[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square\n[prs]: http://makeapullrequest.com\n[donate-badge]: https://img.shields.io/badge/$-support-green.svg?style=flat-square\n[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square\n[coc]: https://github.com/GabrielDuarteM/simple-ws-wrapper/blob/master/other/CODE_OF_CONDUCT.md\n[github-watch-badge]: https://img.shields.io/github/watchers/GabrielDuarteM/simple-ws-wrapper.svg?style=social\n[github-watch]: https://github.com/GabrielDuarteM/simple-ws-wrapper/watchers\n[github-star-badge]: https://img.shields.io/github/stars/GabrielDuarteM/simple-ws-wrapper.svg?style=social\n[github-star]: https://github.com/GabrielDuarteM/simple-ws-wrapper/stargazers\n[twitter]: https://twitter.com/intent/tweet?text=Check%20out%20simple-ws-wrapper%20by%20%40GabrielDuarteM%20https%3A%2F%2Fgithub.com%2FGabrielDuarteM%2Fsimple-ws-wrapper%20%F0%9F%91%8D\n[twitter-badge]: https://img.shields.io/twitter/url/https/github.com/GabrielDuarteM/simple-ws-wrapper.svg?style=social\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgabeduartem%2Fsimple-ws-wrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgabeduartem%2Fsimple-ws-wrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgabeduartem%2Fsimple-ws-wrapper/lists"}