{"id":23183468,"url":"https://github.com/danielkov/wsocket.io-client","last_synced_at":"2025-04-05T03:43:51.317Z","repository":{"id":57400091,"uuid":"75495285","full_name":"danielkov/wsocket.io-client","owner":"danielkov","description":"Client only for wsocket.io ws wrapper.","archived":false,"fork":false,"pushed_at":"2016-12-07T15:10:33.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-11T18:56:47.494Z","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-12-03T19:25:19.000Z","updated_at":"2016-12-03T23:29:50.000Z","dependencies_parsed_at":"2022-09-19T01:10:24.406Z","dependency_job_id":null,"html_url":"https://github.com/danielkov/wsocket.io-client","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Fwsocket.io-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Fwsocket.io-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Fwsocket.io-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Fwsocket.io-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielkov","download_url":"https://codeload.github.com/danielkov/wsocket.io-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247284918,"owners_count":20913691,"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":[],"created_at":"2024-12-18T09:13:08.052Z","updated_at":"2025-04-05T03:43:51.302Z","avatar_url":"https://github.com/danielkov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Wsocket.io Client\n\nThis is a client-side wrapper for the WebSocket native browser API. It makes it a lot easier to use WebSockets with Node JS by providing a simple to use API. Take a look at [wsocket.io-server](https://github.com/danielkov/wsocket.io-server) or [wsocket.io](https://github.com/danielkov/wsocket.io) for a full integration example.\n\n### Browser support\n\nClient code needs to be transpiled with something like [Babel](https://babeljs.io/) to work in all browsers. Currently only browsers supporting WebSocket API are supported (no fallback to AJAX).\n\n## Supported methods\n\n### API\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. Example usage:\n\n```js\nimport WSClient from 'wsocket.io-client';\n\nconst ws = new WSClient('ws://www.myamazingwebsite.com:4200');\n```\n\n### `.on([name: String], [fn: Function | Array \u003ccallback\u003e])`\nHandles incoming messages matching the name provided in the first parameter. This method supports subscription to multiple events via space-separated names. The callback to execute can be an anonymous or named function, an array of functions or multiple functions as well. Example usage:\n\n```js\nfunction handleWsEvents (data) {\n  document.querySelector('#log-screen').innerHTML = data.message;\n}\n\nws.on('message signedup userloggedin', data =\u003e {\n  console.log(`Data received: ${data}`);\n}, handleWsEvents);\n```\n\nThis method returns `this`, for easy chaining.\nNote, you can subscribe to events that already have a handler attached to them, which will not overwrite but also include the handler you passed into the method. Example:\n\n```js\nws.on('message userloggedin signedup', data =\u003e {\n  console.log(data);\n})\n.on('message', data =\u003e {\n  console.log(`Received message: ${data.message}!`)\n}, data =\u003e {\n  User.messages.push(data);\n})\n```\n\n### `.all([fn: Function])`\nSubscribes to every message received via websocket. The callback function is different from the rest, because it receives 2 parameters, the name and the data. This helps distinguish the message type, if a custom form of handling is required for a set of events. This method can accept multiple functions as an argument, in which case each of the functions is going to be executed in order. Example usage:\n\n```js\nws.all((name, data) =\u003e {\n  if (name.startsWith('message')) {\n    console.log(`We received a new message: ${data}`);\n  }\n  else {\n    console.log(`We received an unknown WebSocket message: ${data}`);\n  }\n})\n```\n\nThis method returns `this`, for easy chaining.\n\n### `.offAll()`\nRemoves all event listeners from the WebSocket connection. Example usage:\n\n```js\nws.offAll(); // Stops listening to events without closing the connection.\n```\n\nThis method returns `this`, for easy chaining.\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. Example usage:\n\n```js\nws.send('message', {user: 'John', message: 'Hey there', emote: 'smile'});\n```\n\nThis method returns `this`, for easy chaining.\n\n### `.off([name: String])`\nRemoves all event handlers associated with the name provided in the parameters. This functions supports removing events from multiple listeners. Example usage:\n\n```js\nws.off('message userloggedin'); // Unsubscribes all messages with the names: 'message' and 'userloggedin'.\n```\n\nThis method returns `this`, for easy chaining.\n\n### `.close([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\nThis method returns `this`, for easy chaining.\n\n### Extending the Constructor class\nBecause both the server, the socket and the client are just ES6 classes, all ES6 features also work on them by standard. For example you could do something like this:\n\n```js\nimport { Client } from 'wsocket.io-client';\n\n// This is the minimum requirement for extending the class.\nclass AwesomeClient extends Client {\n  constructor () {\n    super('ws://www.awesome-only.com:4200');\n  }\n}\n\nlet aws = new AwesomeClient();\n\naws.on('awesome-message', data =\u003e {\n  console.log('Received an awesome message: ' + data.message);\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielkov%2Fwsocket.io-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielkov%2Fwsocket.io-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielkov%2Fwsocket.io-client/lists"}