🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

eventsource-client

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eventsource-client

Modern EventSource client for browsers and Node.js

latest
Source
npmnpm
Version
1.2.0
Version published
Maintainers
1
Created
Source

eventsource-client

npm versionnpm bundle size

A modern, streaming client for server-sent events/eventsource.

Another one?

Yes! There are indeed lots of different EventSource clients and polyfills out there. In fact, I am a co-maintainer of the most popular one. This one is different in a few ways, however:

Installation

npm install --save eventsource-client

Supported engines

  • Node.js >= 18
  • Chrome >= 63
  • Safari >= 11.3
  • Firefox >= 65
  • Edge >= 79
  • Deno >= 1.30
  • Bun >= 1.1.23

Basically, any environment that supports:

Usage (async iterator)

import {createEventSource} from 'eventsource-client'

const es = createEventSource({
  url: 'https://my-server.com/sse',

  // your `fetch()` implementation of choice, or `globalThis.fetch` if not set
  fetch: myFetch,
})

let seenMessages = 0
for await (const {data, event, id} of es) {
  console.log('Data: %s', data)
  console.log('Event ID: %s', id) // Note: can be undefined
  console.log('Event: %s', event) // Note: can be undefined

  if (++seenMessages === 10) {
    break
  }
}

// IMPORTANT: EventSource is _not_ closed automatically when breaking out of
// loop. You must manually call `close()` to close the connection.
es.close()

Usage (onMessage callback)

import {createEventSource} from 'eventsource-client'

const es = createEventSource({
  url: 'https://my-server.com/sse',

  onMessage: ({data, event, id}) => {
    console.log('Data: %s', data)
    console.log('Event ID: %s', id) // Note: can be undefined
    console.log('Event: %s', event) // Note: can be undefined
  },

  // your `fetch()` implementation of choice, or `globalThis.fetch` if not set
  fetch: myFetch,
})

console.log(es.readyState) // `open`, `closed` or `connecting`
console.log(es.lastEventId)

// Later, to terminate and prevent reconnections:
es.close()

Minimal usage

import {createEventSource} from 'eventsource-client'

const es = createEventSource('https://my-server.com/sse')
for await (const {data} of es) {
  console.log('Data: %s', data)
}

Comments

Should you need to read/respond to comments, pass an onComment callback:

import {createEventSource} from 'eventsource-client'

const es = createEventSource({
  url: 'https://my-server.com/sse',
  onComment: (comment: string) => {
    /* a single, leading space will be trimmed if present */
    /* eg `: hello` and `:hello` will both yield `hello` */
  },
})

Todo

  • Figure out what to do on broken connection on request body
  • Configurable stalled connection detection (eg no data)
  • Configurable reconnection policy
  • Consider legacy build

License

MIT © Espen Hovlandsdal

Keywords

sse

FAQs

Package last updated on 19 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts