{"id":22280969,"url":"https://github.com/eventsource/eventsource","last_synced_at":"2025-05-11T05:48:14.339Z","repository":{"id":2418459,"uuid":"3386825","full_name":"EventSource/eventsource","owner":"EventSource","description":"EventSource client for Node.js, browsers and other JavaScript runtimes","archived":false,"fork":false,"pushed_at":"2025-05-09T11:15:40.000Z","size":1348,"stargazers_count":1019,"open_issues_count":2,"forks_count":258,"subscribers_count":32,"default_branch":"main","last_synced_at":"2025-05-11T05:48:06.216Z","etag":null,"topics":["eventsource","server-sent-events","sse"],"latest_commit_sha":null,"homepage":"","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/EventSource.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-02-08T12:03:28.000Z","updated_at":"2025-05-10T17:56:44.000Z","dependencies_parsed_at":"2023-01-14T11:01:31.426Z","dependency_job_id":"f361b64d-bc6c-45b3-8db6-72f9e8233ad3","html_url":"https://github.com/EventSource/eventsource","commit_stats":{"total_commits":294,"total_committers":55,"mean_commits":5.345454545454546,"dds":0.5578231292517006,"last_synced_commit":"79dacfa860751438c116df76f9c5fdb9a2a3d69f"},"previous_names":["aslakhellesoy/eventsource"],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EventSource%2Feventsource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EventSource%2Feventsource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EventSource%2Feventsource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EventSource%2Feventsource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EventSource","download_url":"https://codeload.github.com/EventSource/eventsource/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253523733,"owners_count":21921818,"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":["eventsource","server-sent-events","sse"],"created_at":"2024-12-03T16:11:09.043Z","updated_at":"2025-05-11T05:48:14.302Z","avatar_url":"https://github.com/EventSource.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# eventsource\n\n[![npm version](https://img.shields.io/npm/v/eventsource.svg?style=flat-square)](https://www.npmjs.com/package/eventsource)[![npm bundle size](https://img.shields.io/bundlephobia/minzip/eventsource?style=flat-square)](https://bundlephobia.com/result?p=eventsource)[![npm weekly downloads](https://img.shields.io/npm/dw/eventsource.svg?style=flat-square)](https://www.npmjs.com/package/eventsource)\n\nWhatWG/W3C-compatible [server-sent events/eventsource](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) client. The module attempts to implement an absolute minimal amount of features/changes beyond the specification.\n\nIf you're looking for a modern alternative with a less constrained API, check out the [`eventsource-client` package](https://www.npmjs.com/package/eventsource-client).\n\n## Installation\n\n```bash\nnpm install --save eventsource\n```\n\n## Supported engines\n\n- Node.js \u003e= 18\n- Chrome \u003e= 63\n- Safari \u003e= 11.3\n- Firefox \u003e= 65\n- Edge \u003e= 79\n- Deno \u003e= 1.30\n- Bun \u003e= 1.1.23\n\nBasically, any environment that supports:\n\n- [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch)\n- [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)\n- [TextDecoderStream](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream)\n- [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL)\n- [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event), [MessageEvent](https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent), [EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)\n\nIf you need to support older runtimes, try the `2.x` branch/version range (note: 2.x branch is primarily targetted at Node.js, not browsers).\n\n## Usage\n\n```ts\nimport {EventSource} from 'eventsource'\n\nconst es = new EventSource('https://my-server.com/sse')\n\n/*\n * This will listen for events with the field `event: notice`.\n */\nes.addEventListener('notice', (event) =\u003e {\n  console.log(event.data)\n})\n\n/*\n * This will listen for events with the field `event: update`.\n */\nes.addEventListener('update', (event) =\u003e {\n  console.log(event.data)\n})\n\n/*\n * The event \"message\" is a special case, as it will capture events _without_ an\n * event field, as well as events that have the specific type `event: message`.\n * It will not trigger on any other event type.\n */\nes.addEventListener('message', (event) =\u003e {\n  console.log(event.data)\n})\n\n/**\n * To explicitly close the connection, call the `close` method.\n * This will prevent any reconnection from happening.\n */\nsetTimeout(() =\u003e {\n  es.close()\n}, 10_000)\n```\n\n### TypeScript\n\nMake sure you have configured your TSConfig so it matches the environment you are targetting. If you are targetting browsers, this would be `dom`:\n\n```jsonc\n{\n  \"compilerOptions\": {\n    \"lib\": [\"dom\"],\n  },\n}\n```\n\nIf you're using Node.js, ensure you have `@types/node` installed (and it is version 18 or higher). Cloudflare workers have `@cloudflare/workers-types` etc.\n\nThe following errors are caused by targetting an environment that does not have the necessary types available:\n\n```\nerror TS2304: Cannot find name 'Event'.\nerror TS2304: Cannot find name 'EventTarget'.\nerror TS2304: Cannot find name 'MessageEvent'.\n```\n\n## Migrating from v1 / v2\n\nSee [MIGRATION.md](MIGRATION.md#v2-to-v3) for a detailed migration guide.\n\n## Extensions to the WhatWG/W3C API\n\n### Message and code properties on errors\n\nThe `error` event has a `message` and `code` property that can be used to get more information about the error. In the specification, the Event\n\n```ts\nes.addEventListener('error', (err) =\u003e {\n  if (err.code === 401 || err.code === 403) {\n    console.log('not authorized')\n  }\n})\n```\n\n### Specify `fetch` implementation\n\nThe `EventSource` constructor accepts an optional `fetch` property in the second argument that can be used to specify the `fetch` implementation to use.\n\nThis can be useful in environments where the global `fetch` function is not available - but it can also be used to alter the request/response behaviour.\n\n#### Setting HTTP request headers\n\n```ts\nconst es = new EventSource('https://my-server.com/sse', {\n  fetch: (input, init) =\u003e\n    fetch(input, {\n      ...init,\n      headers: {\n        ...init.headers,\n        Authorization: 'Bearer myToken',\n      },\n    }),\n})\n```\n\n#### HTTP/HTTPS proxy\n\nUse a package like [`node-fetch-native`](https://github.com/unjs/node-fetch-native) to add proxy support, either through environment variables or explicit configuration.\n\n```ts\n// npm install node-fetch-native --save\nimport {fetch} from 'node-fetch-native/proxy'\n\nconst es = new EventSource('https://my-server.com/sse', {\n  fetch: (input, init) =\u003e fetch(input, init),\n})\n```\n\n#### Allow unauthorized HTTPS requests\n\nUse a package like [`undici`](https://github.com/nodejs/undici) for more control of fetch options through the use of an [`Agent`](https://undici.nodejs.org/#/docs/api/Agent.md).\n\n```ts\n// npm install undici --save\nimport {fetch, Agent} from 'undici'\n\nawait fetch('https://my-server.com/sse', {\n  dispatcher: new Agent({\n    connect: {\n      rejectUnauthorized: false,\n    },\n  }),\n})\n```\n\n## License\n\nMIT-licensed. See [LICENSE](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feventsource%2Feventsource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feventsource%2Feventsource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feventsource%2Feventsource/lists"}