{"id":25934389,"url":"https://github.com/sanand0/asyncsse","last_synced_at":"2025-07-30T03:15:09.575Z","repository":{"id":257827128,"uuid":"872955045","full_name":"sanand0/asyncsse","owner":"sanand0","description":"Fetch Server-Sent Events (SSE) as an async iterable","archived":false,"fork":false,"pushed_at":"2025-06-09T02:33:17.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-09T21:38:10.317Z","etag":null,"topics":["library","tool"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/asyncsse","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/sanand0.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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,"publiccode":null,"codemeta":null}},"created_at":"2024-10-15T11:11:31.000Z","updated_at":"2025-06-09T02:33:14.000Z","dependencies_parsed_at":"2024-12-25T10:48:44.869Z","dependency_job_id":"188f2123-0280-4601-9d73-102d4035e3fb","html_url":"https://github.com/sanand0/asyncsse","commit_stats":null,"previous_names":["gramener/asyncsse","sanand0/asyncsse"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/sanand0/asyncsse","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanand0%2Fasyncsse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanand0%2Fasyncsse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanand0%2Fasyncsse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanand0%2Fasyncsse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sanand0","download_url":"https://codeload.github.com/sanand0/asyncsse/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanand0%2Fasyncsse/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267803275,"owners_count":24146517,"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-07-30T02:00:09.044Z","response_time":70,"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":["library","tool"],"created_at":"2025-03-04T00:57:40.495Z","updated_at":"2025-07-30T03:15:09.561Z","avatar_url":"https://github.com/sanand0.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# asyncSSE\n\n[![npm version](https://img.shields.io/npm/v/asyncsse.svg)](https://www.npmjs.com/package/asyncsse)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![bundle size](https://img.shields.io/bundlephobia/minzip/asyncsse)](https://bundlephobia.com/package/asyncsse)\n\nFetch Server-Sent Events (SSE) as an async iterable.\n\n## Features\n\n- 🚀 Lightweight (\u003c1KB) and dependency-free\n- 🔄 Works with any SSE-compatible API\n- 🌐 Browser and Node.js compatible\n- 📦 Easy to use with ES modules\n\n## Installation\n\n```bash\nnpm install asyncsse\n```\n\n## Usage\n\n### Browser (via CDN)\n\n```html\n\u003cscript type=\"module\"\u003e\n  import { asyncSSE } from \"https://cdn.jsdelivr.net/npm/asyncsse@1\";\n\n  // Example usage\n  (async () =\u003e {\n    for await (const { data, error } of asyncSSE(\"https://api.example.com/sse\")) {\n      if (error) throw new Error(error);\n      console.log(data);\n    }\n  })();\n\u003c/script\u003e\n```\n\n### Node.js or bundled projects\n\n```javascript\nimport { asyncSSE } from \"asyncsse\";\n\n// Example usage\n(async () =\u003e {\n  for await (const { data, error } of asyncSSE(\"https://api.example.com/sse\")) {\n    if (error) throw new Error(error);\n    console.log(data);\n  }\n})();\n```\n\n## API\n\n### `asyncSSE(url: string, options?: RequestInit, config?: SSEConfig): AsyncIterable\u003cSSEEvent\u003e`\n\nFetches Server-Sent Events from the specified URL and returns an async iterable.\n\n- `url`: The URL to fetch SSE from\n- `options`: Optional [fetch options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters)\n- `config`: Optional configuration object\n  - `fetch`: Custom fetch implementation (defaults to global fetch)\n  - `onResponse`: Async callback to inspect or modify the Response before streaming begins\n\nReturns an async iterable that yields `SSEEvent` objects.\n\nEvents can have fields like `event`, `data`, `id`, and `retry` like this:\n\n```\nevent: userupdate\nid: 123\ndata: {\"username\": \"testuser\", \"status\": \"online\"}\nretry: 5000\n\ndata: Simple message\n```\n\n`asyncSSE` parses these into a single object.\n\n```javascript\n(async () =\u003e {\n  for await (const event of asyncSSE(sseStream, {})) {\n    console.log(event.event, event.data, event.id, event.retry);\n  }\n})();\n```\n\nThis would output:\n\n```\n{ event: \"userupdate\", id: \"123\", data: '{\"username\": \"testuser\", \"status\": \"online\"}', retry: \"5000\" }\n{ data: \"Simple message\" }\n```\n\n**Comment lines** with a colon (`:`) are comments and are ignored by the parser. They will not result in an event.\n\n```\n: This is a comment, it will be ignored\ndata: First event\n: Another comment\nevent: important\ndata: Second event\nid: 456\n```\n\nThis would output:\n\n```\n{ data: \"First event\" }\n{ event: \"important\", data: \"Second event\", id: \"456\" }\n```\n\n**`retry` fields** suggest a reconnection time (in milliseconds). `asyncSSE` itself does not automatically handle reconnection based on this value; it simply parses and provides it. Wait for this duration and reconnect if the connection is lost.\n\n## Example: OpenAI Chat Completions\n\n```javascript\nimport { asyncSSE } from \"asyncsse\";\n\nconst apiKey = \"YOUR_OPENAI_API_KEY\";\nconst url = \"https://api.openai.com/v1/chat/completions\";\n\nconst options = {\n  method: \"POST\",\n  headers: {\n    \"Content-Type\": \"application/json\",\n    Authorization: `Bearer ${apiKey}`,\n  },\n  body: JSON.stringify({\n    model: \"gpt-4\",\n    stream: true,\n    messages: [{ role: \"user\", content: \"Hello, world!\" }],\n  }),\n};\n\nconst config = {\n  onResponse: async (response) =\u003e {\n    console.log(\"Requests remaining:\", response.headers.get(\"X-Ratelimit-Remaining-Requests\"));\n  },\n};\n\n// Fetch the stream, event by event\nfor await (const { data, error } of asyncSSE(url, options, config)) {\n  if (error) throw new Error(error);\n  console.log(JSON.parse(data));\n}\n```\n\n## Testing with Text Input\n\nYou can directly stream SSE events from a text string using the provided `fetchText` helper:\n\n```javascript\nimport { asyncSSE } from \"https://cdn.jsdelivr.net/npm/asyncsse@1\";\nimport { fetchText } from \"https://cdn.jsdelivr.net/npm/asyncsse@1/dist/fetchtext.js\";\nYou can run the tests using Deno:\n\nconst text = \"data: Hello\\n\\ndata: World\\n\\n\";\n\n// Stream events from text\nfor await (const event of asyncSSE(text, {}, { fetch: fetchText })) {\n  console.log(event);\n}\n```\n\nThis outputs:\n\n```\n{ data: \"Hello\" }\n{ data: \"World\" }\n```\n\nThis is particularly useful for testing SSE parsing without making actual HTTP requests.\n\n## Tests\n\nYou can run the tests using Deno:\n\n```bash\ndeno test --allow-net\n```\n\n## Changelog\n\n- 1.3.3: Improved documentation for error handling and added more examples.\n- 1.3.2: Update repo links\n- 1.3.1: Add `fetchText` helper for mocking SSE responses. Add source maps and TypeScript\n- 1.2.1: Add `config.fetch` parameter for custom fetch implementations\n- 1.1.0: Add `config.onResponse` callback\n- 1.0.0: Initial release\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanand0%2Fasyncsse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsanand0%2Fasyncsse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanand0%2Fasyncsse/lists"}