On this page

⚠️ Warning: the EventSource API is experimental.

Undici exposes a WHATWG spec-compliant implementation of EventSource for Server-Sent Events.

Undici exports an EventSource class. You can instantiate the EventSource as follows:

import { EventSource } from 'undici'

const eventSource = new EventSource('http://localhost:3000')
eventSource.onmessage = (event) => {
  console.log(event.data)
}

EventSource connects to an HTTP endpoint that responds with a text/event-stream content type. The connection stays open and receives events as the server writes them.

import { createServer } from 'node:http'
import { EventSource } from 'undici'

const server = createServer((request, response) => {
  response.writeHead(200, {
    'content-type': 'text/event-stream',
    'cache-control': 'no-cache',
    connection: 'keep-alive'
  })

  response.write('event: ping\n')
  response.write('data: connected\n\n')

  const interval = setInterval(() => {
    response.write(`data: ${Date.now()}\n\n`)
  }, 1000)

  request.on('close', () => clearInterval(interval))
})

server.listen(3000, () => {
  const eventSource = new EventSource('http://localhost:3000')

  eventSource.addEventListener('ping', (event) => {
    console.log('ping:', event.data)
  })

  eventSource.onmessage = (event) => {
    console.log('message:', event.data)
  }

  eventSource.onerror = () => {
    eventSource.close()
    server.close()
  }
})

The message event receives events without an explicit event: field. Use addEventListener() to subscribe to named events.

Undici allows you to set your own Dispatcher in the EventSource constructor.

An example which allows you to modify the request headers is:

import { EventSource, Agent } from 'undici'

class CustomHeaderAgent extends Agent {
  dispatch (opts) {
    opts.headers['x-custom-header'] = 'hello world'
    return super.dispatch(...arguments)
  }
}

const eventSource = new EventSource('http://localhost:3000', {
  dispatcher: new CustomHeaderAgent()
})

More information about the EventSource API can be found on MDN.