{
  "type": "module",
  "source": "doc/api/api-eventsource.md",
  "modules": [
    {
      "textRaw": "EventSource",
      "name": "eventsource",
      "introduced_in": "v6.5.0",
      "type": "module",
      "stability": 1,
      "stabilityText": "Experimental",
      "desc": "<p><code>EventSource</code> is a <a href=\"https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events\">WHATWG-conformant</a> implementation of the\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events\">server-sent events</a> interface. It opens a persistent HTTP connection to a\nserver that responds with the <code>text/event-stream</code> content type and dispatches\nthe events it receives without closing the connection.</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>\n<p>Constructing an <code>EventSource</code> for the first time emits a one-time\n<code>ExperimentalWarning</code> with the code <code>'UNDICI-ES'</code>. The interface is also\ninstalled onto <code>globalThis</code> as <code>globalThis.EventSource</code>.</p>",
      "classes": [
        {
          "textRaw": "Class: `EventSource`",
          "name": "EventSource",
          "type": "class",
          "meta": {
            "added": [
              "v6.5.0"
            ],
            "changes": []
          },
          "stability": 1,
          "stabilityText": "Experimental",
          "desc": "<ul>\n<li>Extends: <a href=\"https://developer.mozilla.org/docs/Web/API/EventTarget\"><code>&#x3C;EventTarget></code></a></li>\n</ul>\n<p>The <code>EventSource</code> interface receives server-sent events over an HTTP\nconnection. Instances are created with the <a href=\"#new-eventsourceurl-eventsourceinitdict\"><code>new EventSource()</code></a>\nconstructor and emit <a href=\"#event-open\"><code>'open'</code></a>, <a href=\"#event-message\"><code>'message'</code></a>, and\n<a href=\"#event-error\"><code>'error'</code></a> events.</p>",
          "signatures": [
            {
              "textRaw": "`new EventSource(url[, eventSourceInitDict])`",
              "name": "EventSource",
              "type": "ctor",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": []
              },
              "stability": 0,
              "stabilityText": "Deprecated. Use `node.dispatcher` instead.",
              "params": [
                {
                  "textRaw": "`url` {string|URL} The URL of the event stream. Relative URLs are resolved against the environment's base URL.",
                  "name": "url",
                  "type": "string|URL",
                  "desc": "The URL of the event stream. Relative URLs are resolved against the environment's base URL."
                },
                {
                  "textRaw": "`eventSourceInitDict` {Object} (optional)",
                  "name": "eventSourceInitDict",
                  "type": "Object",
                  "desc": "(optional)",
                  "options": [
                    {
                      "textRaw": "`withCredentials` {boolean} When `true`, the request is made with the credentials mode set to `include` and CORS attribute state set to `use-credentials`; otherwise the credentials mode is `same-origin`. **Default:** `false`.",
                      "name": "withCredentials",
                      "type": "boolean",
                      "default": "`false`",
                      "desc": "When `true`, the request is made with the credentials mode set to `include` and CORS attribute state set to `use-credentials`; otherwise the credentials mode is `same-origin`."
                    },
                    {
                      "textRaw": "`dispatcher` {Dispatcher} The dispatcher used for the underlying request. **Default:** the global dispatcher.Stability: 0 - Deprecated. Use `node.dispatcher` instead.",
                      "name": "dispatcher",
                      "type": "Dispatcher",
                      "default": "the global dispatcher.Stability: 0 - Deprecated. Use `node.dispatcher` instead",
                      "desc": "The dispatcher used for the underlying request."
                    },
                    {
                      "textRaw": "`node` {Object} undici-specific extensions to the standard `EventSourceInit` dictionary.",
                      "name": "node",
                      "type": "Object",
                      "desc": "undici-specific extensions to the standard `EventSourceInit` dictionary.",
                      "options": [
                        {
                          "textRaw": "`dispatcher` {Dispatcher} The dispatcher used for the underlying request. **Default:** the global dispatcher.",
                          "name": "dispatcher",
                          "type": "Dispatcher",
                          "default": "the global dispatcher",
                          "desc": "The dispatcher used for the underlying request."
                        },
                        {
                          "textRaw": "`reconnectionTime` {number} The reconnection time, in milliseconds, to wait before re-establishing a dropped connection. The server may override this value with a `retry` field. **Default:** `3000`.",
                          "name": "reconnectionTime",
                          "type": "number",
                          "default": "`3000`",
                          "desc": "The reconnection time, in milliseconds, to wait before re-establishing a dropped connection. The server may override this value with a `retry` field."
                        }
                      ]
                    }
                  ],
                  "optional": true
                }
              ],
              "desc": "<blockquote>\n<p>Stability: 1 - Experimental</p>\n</blockquote>\n<p>Creates a new <code>EventSource</code> and immediately begins connecting to <code>url</code>. The\nrequest is sent with the <code>Accept: text/event-stream</code> header, a cache mode of\n<code>no-store</code>, and an initiator type of <code>other</code>.</p>\n<p>If <code>url</code> cannot be parsed, a <code>SyntaxError</code> <code>DOMException</code> is thrown.</p>\n<pre><code class=\"language-mjs\">import { EventSource } from 'undici'\n\nconst eventSource = new EventSource('http://localhost:3000', {\n  withCredentials: true\n})\n</code></pre>\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  node: {\n    dispatcher: new CustomHeaderAgent()\n  }\n})\n</code></pre>"
            }
          ],
          "methods": [
            {
              "textRaw": "`eventSource.close()`",
              "name": "close",
              "type": "method",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": []
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "signatures": [
                {
                  "params": [],
                  "return": {
                    "textRaw": "Returns: {undefined}",
                    "name": "return",
                    "type": "undefined"
                  }
                }
              ],
              "desc": "<p>Closes the connection, if any, aborts the underlying request, and sets\n<a href=\"#eventsourcereadystate\"><code>eventSource.readyState</code></a> to <code>CLOSED</code>. Once closed, the\n<code>EventSource</code> does not attempt to reconnect. Calling <code>close()</code> on an already\nclosed <code>EventSource</code> has no effect.</p>"
            }
          ],
          "properties": [
            {
              "textRaw": "Type: {number}",
              "name": "readyState",
              "type": "number",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": []
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "desc": "<p>A read-only number representing the state of the connection. It is one of the\nfollowing constants:</p>\n<ul>\n<li><a href=\"#eventsourceconnecting\"><code>EventSource.CONNECTING</code></a> (<code>0</code>) — the connection has\nnot yet been established, or it was closed and is being re-established.</li>\n<li><a href=\"#eventsourceopen\"><code>EventSource.OPEN</code></a> (<code>1</code>) — the connection is open and\nevents are being dispatched as they are received.</li>\n<li><a href=\"#eventsourceclosed\"><code>EventSource.CLOSED</code></a> (<code>2</code>) — the connection is not open\nand is not being re-established.</li>\n</ul>"
            },
            {
              "textRaw": "Type: {string}",
              "name": "url",
              "type": "string",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": []
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "desc": "<p>A read-only string providing the URL of the event stream, after resolution\nagainst the environment's base URL.</p>"
            },
            {
              "textRaw": "Type: {boolean}",
              "name": "withCredentials",
              "type": "boolean",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": []
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "desc": "<p>A read-only boolean indicating whether the <code>EventSource</code> was instantiated with\nCORS credentials (<code>true</code>), or not (<code>false</code>, the default). It reflects the\n<code>withCredentials</code> option passed to the constructor.</p>"
            },
            {
              "textRaw": "Type: {Function|null} **Default:** `null`.",
              "name": "onopen",
              "type": "Function|null",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": []
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "default": "`null`",
              "desc": "<p>An event handler that is invoked when an <a href=\"#event-open\"><code>'open'</code></a> event is\ndispatched. Assigning a function registers it as the handler; assigning <code>null</code>\nremoves the current handler.</p>"
            },
            {
              "textRaw": "Type: {Function|null} **Default:** `null`.",
              "name": "onmessage",
              "type": "Function|null",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": []
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "default": "`null`",
              "desc": "<p>An event handler that is invoked when a <a href=\"#event-message\"><code>'message'</code></a> event is\ndispatched. Assigning a function registers it as the handler; assigning <code>null</code>\nremoves the current handler.</p>"
            },
            {
              "textRaw": "Type: {Function|null} **Default:** `null`.",
              "name": "onerror",
              "type": "Function|null",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": []
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "default": "`null`",
              "desc": "<p>An event handler that is invoked when an <a href=\"#event-error\"><code>'error'</code></a> event is\ndispatched. Assigning a function registers it as the handler; assigning <code>null</code>\nremoves the current handler.</p>"
            },
            {
              "textRaw": "Type: {number}",
              "name": "CONNECTING",
              "type": "number",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": []
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "desc": "<p>The numeric constant <code>0</code>, representing the <code>CONNECTING</code>\n<a href=\"#eventsourcereadystate\"><code>readyState</code></a>. It is defined as a read-only,\nnon-writable property on both the <code>EventSource</code> constructor and instances.</p>"
            },
            {
              "textRaw": "Type: {number}",
              "name": "OPEN",
              "type": "number",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": []
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "desc": "<p>The numeric constant <code>1</code>, representing the <code>OPEN</code>\n<a href=\"#eventsourcereadystate\"><code>readyState</code></a>. It is defined as a read-only,\nnon-writable property on both the <code>EventSource</code> constructor and instances.</p>"
            },
            {
              "textRaw": "Type: {number}",
              "name": "CLOSED",
              "type": "number",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": []
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "desc": "<p>The numeric constant <code>2</code>, representing the <code>CLOSED</code>\n<a href=\"#eventsourcereadystate\"><code>readyState</code></a>. It is defined as a read-only,\nnon-writable property on both the <code>EventSource</code> constructor and instances.</p>"
            }
          ],
          "events": [
            {
              "textRaw": "Event: `'open'`",
              "name": "open",
              "type": "event",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": []
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "params": [],
              "desc": "<p>Emitted when the connection is established and the\n<a href=\"#eventsourcereadystate\"><code>readyState</code></a> becomes <code>OPEN</code>. The listener receives an\n<a href=\"https://developer.mozilla.org/docs/Web/API/Event\"><code>&#x3C;Event></code></a>.</p>\n<pre><code class=\"language-mjs\">import { EventSource } from 'undici'\n\nconst eventSource = new EventSource('http://localhost:3000')\neventSource.addEventListener('open', () => {\n  console.log('connection opened')\n})\n</code></pre>"
            },
            {
              "textRaw": "Event: `'message'`",
              "name": "message",
              "type": "event",
              "meta": {
                "added": [
                  "v6.15.0"
                ],
                "changes": []
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "params": [],
              "desc": "<p>Emitted when a message that has no explicit <code>event</code> field is received. The\nlistener receives a <a href=\"https://developer.mozilla.org/docs/Web/API/MessageEvent\"><code>&#x3C;MessageEvent></code></a> whose <code>data</code>, <code>lastEventId</code>, and <code>origin</code>\nproperties are populated from the server-sent event. Named events (those with an\n<code>event</code> field) are dispatched under their own type and must be subscribed to with\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener\"><code>addEventListener()</code></a>.</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  // Named event, delivered under its own type.\n  eventSource.addEventListener('ping', (event) => {\n    console.log('ping:', event.data)\n  })\n\n  // Unnamed event, delivered as 'message'.\n  eventSource.onmessage = (event) => {\n    console.log('message:', event.data)\n  }\n})\n</code></pre>"
            },
            {
              "textRaw": "Event: `'error'`",
              "name": "error",
              "type": "event",
              "meta": {
                "added": [
                  "v6.5.0"
                ],
                "changes": [
                  {
                    "version": "v7.11.0",
                    "pr-url": "https://github.com/nodejs/undici/pull/4247",
                    "description": "The connection is no longer re-established after a network error; the `EventSource` is closed instead."
                  }
                ]
              },
              "stability": 1,
              "stabilityText": "Experimental",
              "params": [],
              "desc": "<p>Emitted when the connection fails or is interrupted. The listener receives an\n<a href=\"https://developer.mozilla.org/docs/Web/API/Event\"><code>&#x3C;Event></code></a>. When the failure is recoverable, the <code>EventSource</code> transitions back to\n<code>CONNECTING</code> and retries after the reconnection time; when it is not, the\n<code>EventSource</code> transitions to <code>CLOSED</code> and does not reconnect.</p>\n<pre><code class=\"language-mjs\">import { EventSource } from 'undici'\n\nconst eventSource = new EventSource('http://localhost:3000')\neventSource.onerror = () => {\n  if (eventSource.readyState === EventSource.CLOSED) {\n    console.log('connection closed')\n  } else {\n    console.log('reconnecting')\n  }\n}\n</code></pre>"
            }
          ]
        }
      ]
    }
  ]
}