{"id":27213495,"url":"https://github.com/starmordar/use-shared-sse","last_synced_at":"2025-04-10T02:42:10.719Z","repository":{"id":286804359,"uuid":"962598506","full_name":"Starmordar/use-shared-sse","owner":"Starmordar","description":"React hook that manages a single SSE connection across browser tabs using BroadcastChannel and Web Locks to prevent duplicate connections","archived":false,"fork":false,"pushed_at":"2025-04-08T13:17:46.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-08T13:25:18.802Z","etag":null,"topics":["broadcast-channel","cross-tab","eventsource","multitabs","react","react-hook","server-sent-events","sse","web-locks"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/use-shared-sse","language":"TypeScript","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/Starmordar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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,"publiccode":null,"codemeta":null}},"created_at":"2025-04-08T11:51:27.000Z","updated_at":"2025-04-08T13:18:09.000Z","dependencies_parsed_at":"2025-04-08T13:36:07.745Z","dependency_job_id":null,"html_url":"https://github.com/Starmordar/use-shared-sse","commit_stats":null,"previous_names":["starmordar/use-shared-sse"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Starmordar%2Fuse-shared-sse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Starmordar%2Fuse-shared-sse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Starmordar%2Fuse-shared-sse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Starmordar%2Fuse-shared-sse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Starmordar","download_url":"https://codeload.github.com/Starmordar/use-shared-sse/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248144990,"owners_count":21055026,"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":["broadcast-channel","cross-tab","eventsource","multitabs","react","react-hook","server-sent-events","sse","web-locks"],"created_at":"2025-04-10T02:42:10.068Z","updated_at":"2025-04-10T02:42:10.712Z","avatar_url":"https://github.com/Starmordar.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-shared-sse\n\n![minzipped size](https://badgen.net/bundlephobia/minzip/use-shared-sse)\n![Types](https://badgen.net/badge/TS/TypeScript/blue)\n[![npm version](https://badgen.net/npm/v/use-shared-sse)](https://www.npmjs.com/package/use-shared-sse)\n![License](https://badgen.net/npm/license/use-shared-sse)\n\n\u003e 🔄 React hook to share a single Server-Sent Events (SSE) connection across multiple browser tabs using BroadcastChannel and Web Locks.  \n\u003e ✅ Helps avoid browser HTTP/1.1 connection limits by ensuring only one active SSE connection across all tabs.\n\n## 📦 Installation\n\n```bash\nnpm install use-shared-sse\n```\n\n## 🚀 Quick Start\n\n```tsx\nimport { useSse } from 'use-shared-sse';\n\nfunction SseExample() {\n  useSse({\n    url: 'http://localhost:3005/sse',\n    options: { withCredentials: true },\n    events: [\n      { name: 'ping', cb: onPingEvent },\n      { name: 'hero', cb: onHeroReceiveEvent },\n    ],\n  });\n\n  function onPingEvent(data: MessageEvent['data']) {\n    console.log('Ping:', data);\n  }\n\n  function onHeroReceiveEvent(data: MessageEvent['data']) {\n    console.log('Hero:', data);\n  }\n\n  return \u003c\u003e\u003c/\u003e;\n}\n```\n\n## 📚 API Reference\n\n### `useSse(options: UseSseOptions)`\n\nReact hook that manages a shared SSE connection across browser tabs using `BroadcastChannel` and `Web Locks`.\n\n---\n\n### `UseSseOptions`\n\n| Field         | Type                                           | Required | Description |\n|---------------|------------------------------------------------|----------|-------------|\n| `url`         | `string \\| URL`                                | ✅ Yes   | The URL to open the SSE connection to. |\n| `options`     | `EventSourceInit`                              | No       | SSE configuration object. Default is `{ withCredentials: true }`. See [MDN docs](https://developer.mozilla.org/en-US/docs/Web/API/EventSource/EventSource#options). |\n| `events`      | `{ name: string; cb: (event: MessageEvent) =\u003e void }[]` | ✅ Yes   | A list of event listeners, each with an event name and handler. |\n| `channelName` | `string`                                       | No       | Custom name for the `BroadcastChannel`. Default: `\"sse-channel\"`. |\n| `lockName`    | `string`                                       | No       | Custom name for the `Web Lock`. Default: `\"sse-lock\"`. |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarmordar%2Fuse-shared-sse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstarmordar%2Fuse-shared-sse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarmordar%2Fuse-shared-sse/lists"}