{"id":13447738,"url":"https://github.com/lukeed/sockette","last_synced_at":"2025-05-14T07:08:54.400Z","repository":{"id":42136331,"uuid":"99386917","full_name":"lukeed/sockette","owner":"lukeed","description":"The cutest little WebSocket wrapper! 🧦","archived":false,"fork":false,"pushed_at":"2024-01-20T17:07:08.000Z","size":61,"stargazers_count":2468,"open_issues_count":11,"forks_count":81,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-05-04T03:34:50.447Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/lukeed.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":"lukeed"}},"created_at":"2017-08-04T23:39:37.000Z","updated_at":"2025-04-26T21:24:42.000Z","dependencies_parsed_at":"2024-02-04T17:38:37.136Z","dependency_job_id":null,"html_url":"https://github.com/lukeed/sockette","commit_stats":{"total_commits":67,"total_committers":10,"mean_commits":6.7,"dds":"0.13432835820895528","last_synced_commit":"75d9e73e511789ef77b7c6011800fcb0f602b24d"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fsockette","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fsockette/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fsockette/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fsockette/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lukeed","download_url":"https://codeload.github.com/lukeed/sockette/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254092776,"owners_count":22013290,"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-07-31T05:01:25.619Z","updated_at":"2025-05-14T07:08:49.382Z","avatar_url":"https://github.com/lukeed.png","language":"JavaScript","readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/lukeed/sockette/raw/master/sockette.jpg\" alt=\"Sockette\" height=\"250\" /\u003e\n\u003c/div\u003e\n\n\u003ch1 align=\"center\"\u003eSockette\u003c/h1\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://npmjs.org/package/sockette\"\u003e\n    \u003cimg src=\"https://badgen.now.sh/npm/v/sockette\" alt=\"version\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/lukeed/sockette/actions\"\u003e\n    \u003cimg src=\"https://github.com/lukeed/sockette/workflows/CI/badge.svg\" alt=\"CI\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://licenses.dev/npm/sockette\"\u003e\n    \u003cimg src=\"https://licenses.dev/b/npm/sockette\" alt=\"licenses\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://npmjs.org/package/sockette\"\u003e\n    \u003cimg src=\"https://badgen.now.sh/npm/dm/sockette\" alt=\"downloads\" /\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003eThe cutest little WebSocket wrapper! 🧦\u003c/div\u003e\n\n\u003cbr /\u003e\n\nSockette is a tiny (367 bytes) wrapper around `WebSocket` that will automatically reconnect if the connection is lost!\n\nIn addition to attaching [additional API methods](#api), Sockette allows you to **reuse** instances, avoiding the need to redeclare all event listeners.\n\nYou have direct access to the (current) underlying `WebSocket` within every `EventListener` callback (via `event.target`).\n\n\n## Install\n\n```\n$ npm install --save sockette\n```\n\n\n## Usage\n\nUnlike `WebSocket`, you should declare all event listeners on initialization:\n```js\nconst Sockette = require('sockette');\n\nconst ws = new Sockette('ws://localhost:3000', {\n  timeout: 5e3,\n  maxAttempts: 10,\n  onopen: e =\u003e console.log('Connected!', e),\n  onmessage: e =\u003e console.log('Received:', e),\n  onreconnect: e =\u003e console.log('Reconnecting...', e),\n  onmaximum: e =\u003e console.log('Stop Attempting!', e),\n  onclose: e =\u003e console.log('Closed!', e),\n  onerror: e =\u003e console.log('Error:', e)\n});\n\nws.send('Hello, world!');\nws.json({type: 'ping'});\nws.close(); // graceful shutdown\n\n// Reconnect 10s later\nsetTimeout(ws.reconnect, 10e3);\n```\n\n\n## API\n\n### Sockette(url, options)\n\nReturns: `Sockette`\n\nReturns the `Sockette` instance.\n\n#### url\nType: `String`\n\nThe URL you want to connect to \u0026mdash; Should be prefixed with `ws://` or `wss://`. This is passed directly to `WebSocket`.\n\n#### options.protocols\nType: `String|Array`\n\nEither a single protocol string or an array of strings used to indicate sub-protocols. See the [`WebSocket` docs][MDN] for more info.\n\n#### options.timeout\nType: `Number`\u003cbr\u003e\nDefault: `1000`\n\nThe amount of time (in `ms`) to wait in between reconnection attempts. Defaults to 1 second.\n\n#### options.maxAttempts\nType: `Number`\u003cbr\u003e\nDefault: `Infinity`\n\nThe maximum number of attempts to reconnect.\n\n\u003e **Important:** Pass `-1` if you want to disable this feature. Although, this is main reason to use Sockette! :joy:\n\n#### options.onopen\nType: `Function`\n\nThe `EventListener` to run in response to `'open'` events. It receives the `Event` object as its only parameter.\n\n\u003e This is called when the connection has been established and is ready to send and receive data.\n\n\u003e **Important:** Sockette will forget the number of previous reconnection attempts, so that the next time connection is lost, you will consistently retry `n` number of times, as determined by `options.maxAttempts`.\n\n#### options.onmessage\nType: `Function`\n\nThe `EventListener` to run in response to `'message'` events. It receives the `Event` object as its only parameter.\n\n\u003e This is called when a message has been received from the server. You'll probably want `event.data`!\n\n#### options.onreconnect\nType: `Function`\n\nThe callback to run when attempting to reconnect to the server.\n\nIf Sockette is automatically reconnecting in response to an `error` or unexpected `close` event, then your `onreconnect` callback will receive the forwarded `Event` object.\n\n#### options.onmaximum\nType: `Function`\n\nThe callback to run when the [`maxAttempts`](o#ptionsmaxattempts) limit has been met.\n\nThis callback will receive the forwarded `Event` object from `onclose`.\n\n#### options.onclose\nType: `Function`\n\nThe `EventListener` to run in response to `'close'` events. It receives the `Event` object as its only parameter.\n\n\u003e This is called when the connection has been closed for any reason.\n\n\u003e **Important:** If the `event.code` is _not_ `1000`, `1001`, or `1005` an automatic reconnect attempt will be queued.\n\n#### options.onerror\nType: `Function`\n\nThe `EventListener` to run in response to `'error'` events. It receives the `Event` object as its only parameter.\n\n\u003e This is called anytime an error occurs.\n\n\u003e **Important:** If the `event.code` is `ECONNREFUSED`, an automatic reconnect attempt will be queued.\n\n### send(data)\n\nIdentical to [`WebSocket.send()`][send], capable of sending multiple data types.\n\n### close(code, reason)\n\nIdentical to [`WebSocket.close()`][close].\n\n\u003e **Note:** The `code` will default to `1000` unless specified.\n\n### json(obj)\n\nConvenience method that passes your `obj` (Object) through `JSON.stringify` before passing it to [`WebSocket.send()`][send].\n\n### reconnect()\n\nIf [`options.maxAttempts`](#optionsmaxattempts) has not been exceeded, enqueues a reconnection attempt. Otherwise, it runs your [`options.onmaximum`](#optionsonmaximum) callback.\n\n### open()\n\nInitializes a new `WebSocket` \u0026mdash; used on initialization and by [`reconnect()`](#reconnect).\n\n\n## License\n\nMIT © [Luke Edwards](https://lukeed.com)\n\n[MDN]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket\n[close]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close\n[send]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send\n","funding_links":["https://github.com/sponsors/lukeed"],"categories":["JavaScript","Repository","Packages","Tools per Language"],"sub_categories":["Real-time","WebSocket","Node.js"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukeed%2Fsockette","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flukeed%2Fsockette","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukeed%2Fsockette/lists"}