{"id":17220,"library":"eventsource-polyfill","title":"EventSource Polyfill","description":"eventsource-polyfill provides a robust client-side polyfill for the W3C EventSource API, enabling Server-Sent Events (SSE) functionality in web browsers that lack native support, particularly older environments like Internet Explorer 8+ and Android browser 2.1+. The package aims to bridge the compatibility gap, ensuring that web applications can leverage SSE streams consistently across diverse browser landscapes. While the project saw its last significant update with version 0.9.7 in 2017, which addressed a critical bug related to 'data' events, its release cadence has ceased, indicating an abandoned status. Developers should be aware it's designed exclusively for browser applications, modifying the global window.EventSource object, and will not receive further updates or new features.","status":"abandoned","version":"0.9.6","language":"javascript","source_language":"en","source_url":"https://github.com/amvtek/EventSource","tags":["javascript","sse","server sent events","eventsource","event-source","polyfill"],"install":[{"cmd":"npm install eventsource-polyfill","lang":"bash","label":"npm"},{"cmd":"yarn add eventsource-polyfill","lang":"bash","label":"yarn"},{"cmd":"pnpm add eventsource-polyfill","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This polyfill modifies the global window.EventSource object; direct named imports for EventSource are incorrect and will not provide the intended polyfilled constructor.","wrong":"import { EventSource } from 'eventsource-polyfill';","symbol":"Polyfill Activation (ESM)","correct":"import 'eventsource-polyfill';"},{"note":"The module is primarily used for its side effects to patch the global EventSource. The return value of require() is not the polyfilled constructor, which is accessible globally.","wrong":"const EventSource = require('eventsource-polyfill');","symbol":"Polyfill Activation (CJS)","correct":"require('eventsource-polyfill');"}],"quickstart":{"code":"import 'eventsource-polyfill'; // Ensure polyfill is loaded\n\nfunction setupEventSource() {\n  if (typeof window === 'undefined' || typeof EventSource === 'undefined') {\n    console.warn('EventSource is not available in this environment or not polyfilled.');\n    return;\n  }\n\n  // Replace with your actual SSE endpoint\n  const eventSourceUrl = 'https://example.com/stream'; // Or a mock API if running locally\n\n  console.log(`Attempting to connect to EventSource at ${eventSourceUrl}`);\n  const es = new EventSource(eventSourceUrl);\n\n  es.onopen = () => {\n    console.log('EventSource connection opened.');\n  };\n\n  es.onmessage = (event) => {\n    console.log('Received message:', event.data);\n    // You can parse JSON if your events are JSON strings\n    try {\n      const data = JSON.parse(event.data);\n      console.log('Parsed JSON data:', data);\n    } catch (e) {\n      // Not JSON\n    }\n  };\n\n  es.addEventListener('customEvent', (event: MessageEvent) => {\n    console.log('Received custom event \"customEvent\":', event.data);\n  });\n\n  es.onerror = (error) => {\n    console.error('EventSource error:', error);\n    // Attempt to reconnect after a delay, or close if critical\n    es.close();\n    console.log('EventSource closed due to error. Reconnecting in 5 seconds...');\n    setTimeout(setupEventSource, 5000);\n  };\n\n  // Close the connection after some time or based on user action\n  setTimeout(() => {\n    es.close();\n    console.log('EventSource connection closed after 30 seconds.');\n  }, 30000);\n}\n\nsetupEventSource();","lang":"typescript","description":"Demonstrates how to load the `eventsource-polyfill` and then establish and manage a Server-Sent Events (SSE) connection using the globally available `EventSource` constructor, including event handling and error management."},"warnings":[{"fix":"Upgrade to version 0.9.7 or newer to resolve the infinite loop issue.","message":"Versions prior to 0.9.7 contained a critical bug where an event named 'data' would cause an infinite loop in the polyfill, leading to application unresponsiveness.","severity":"breaking","affected_versions":"<0.9.7"},{"fix":"Ensure the package is only bundled for browser targets. For Server-Sent Events in Node.js, use a dedicated Node.js library.","message":"This polyfill is strictly for client-side browser environments and will not function correctly in Node.js or other non-browser JavaScript runtimes.","severity":"gotcha","affected_versions":">=0.9.x"},{"fix":"Evaluate alternatives for more active maintenance if targeting modern browsers or requiring ongoing support. For legacy browser support, proceed with caution and thorough testing.","message":"The `eventsource-polyfill` project is abandoned, with its last update in 2017. It will not receive new features, bug fixes for modern browser issues, or security patches.","severity":"gotcha","affected_versions":">=0.9.7"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure the polyfill is imported and bundled for a browser target. For Node.js, use a native SSE client library.","cause":"Attempting to use `EventSource` in a Node.js environment or before the polyfill has been loaded in a browser.","error":"ReferenceError: EventSource is not defined"},{"fix":"If using CommonJS, use `require('eventsource-polyfill');`. If using ES modules, ensure your project is configured for `type: 'module'` in `package.json` or uses a bundler like Webpack/Rollup with proper ESM handling.","cause":"Using ESM `import` syntax in a CommonJS module or an environment not configured for ES modules (e.g., older Browserify setups without appropriate transforms).","error":"Uncaught SyntaxError: Cannot use import statement outside a module"}],"ecosystem":"npm","meta_description":null}