{
  "type": "module",
  "source": "doc/api/api-eventsource.md",
  "modules": [
    {
      "textRaw": "EventSource",
      "name": "eventsource",
      "type": "module",
      "desc": "<blockquote>\n<p>⚠️ Warning: the EventSource API is experimental.</p>\n</blockquote>\n<p>Undici exposes a WHATWG spec-compliant implementation of <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/EventSource\">EventSource</a>\nfor <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events\">Server-Sent Events</a>.</p>",
      "modules": [
        {
          "textRaw": "Instantiating EventSource",
          "name": "instantiating_eventsource",
          "type": "module",
          "desc": "<p>Undici exports an EventSource class. You can instantiate the EventSource as\nfollows:</p>\n<pre><code class=\"language-mjs\">import { EventSource } from 'undici'\n\nconst eventSource = new EventSource('http://localhost:3000')\neventSource.onmessage = (event) => {\n  console.log(event.data)\n}\n</code></pre>",
          "displayName": "Instantiating EventSource"
        },
        {
          "textRaw": "Receiving events from a server",
          "name": "receiving_events_from_a_server",
          "type": "module",
          "desc": "<p>EventSource connects to an HTTP endpoint that responds with a <code>text/event-stream</code>\ncontent type. The connection stays open and receives events as the server writes\nthem.</p>\n<pre><code class=\"language-mjs\">import { createServer } from 'node:http'\nimport { EventSource } from 'undici'\n\nconst server = createServer((request, response) => {\n  response.writeHead(200, {\n    'content-type': 'text/event-stream',\n    'cache-control': 'no-cache',\n    connection: 'keep-alive'\n  })\n\n  response.write('event: ping\\n')\n  response.write('data: connected\\n\\n')\n\n  const interval = setInterval(() => {\n    response.write(`data: ${Date.now()}\\n\\n`)\n  }, 1000)\n\n  request.on('close', () => clearInterval(interval))\n})\n\nserver.listen(3000, () => {\n  const eventSource = new EventSource('http://localhost:3000')\n\n  eventSource.addEventListener('ping', (event) => {\n    console.log('ping:', event.data)\n  })\n\n  eventSource.onmessage = (event) => {\n    console.log('message:', event.data)\n  }\n\n  eventSource.onerror = () => {\n    eventSource.close()\n    server.close()\n  }\n})\n</code></pre>\n<p>The <code>message</code> event receives events without an explicit <code>event:</code> field. Use\n<code>addEventListener()</code> to subscribe to named events.</p>",
          "displayName": "Receiving events from a server"
        },
        {
          "textRaw": "Using a custom Dispatcher",
          "name": "using_a_custom_dispatcher",
          "type": "module",
          "desc": "<p>Undici allows you to set your own Dispatcher in the EventSource constructor.</p>\n<p>An example which allows you to modify the request headers is:</p>\n<pre><code class=\"language-mjs\">import { EventSource, Agent } from 'undici'\n\nclass CustomHeaderAgent extends Agent {\n  dispatch (opts) {\n    opts.headers['x-custom-header'] = 'hello world'\n    return super.dispatch(...arguments)\n  }\n}\n\nconst eventSource = new EventSource('http://localhost:3000', {\n  dispatcher: new CustomHeaderAgent()\n})\n</code></pre>\n<p>More information about the EventSource API can be found on\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/API/EventSource\">MDN</a>.</p>",
          "displayName": "Using a custom Dispatcher"
        }
      ],
      "displayName": "EventSource"
    }
  ]
}