@c4312/eventsource-umd

中文文档
3.0.5发布于 2025/03/15MITTypeScriptESM + CJS近 30 天下载 6.8万

WhatWG/W3C compliant EventSource client for Node.js and browsers

原始使用文档
文档目录9 节
  1. Installation
  2. Supported engines
  3. Usage
  4. TypeScript
  5. Migrating from v1 / v2
  6. Extensions to the WhatWG/W3C API
  7. Message and code properties on errors
  8. Specify fetch implementation
  9. License

eventsource

npm versionnpm bundle sizenpm weekly downloads

WhatWG/W3C-compatible server-sent events/eventsource client. The module attempts to implement an absolute minimal amount of features/changes beyond the specification.

If you're looking for a modern alternative with a less constrained API, check out the eventsource-client package.

Installation

npm install --save eventsource

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:

If you need to support older runtimes, try the 2.x branch/version range (note: 2.x branch is primarily targetted at Node.js, not browsers).

Usage

import {EventSource} from 'eventsource'

const es = new EventSource('https://my-server.com/sse')

/*
 * This will listen for events with the field `event: notice`.
 */
es.addEventListener('notice', (event) => {
  console.log(event.data)
})

/*
 * This will listen for events with the field `event: update`.
 */
es.addEventListener('update', (event) => {
  console.log(event.data)
})

/*
 * The event "message" is a special case, as it will capture events _without_ an
 * event field, as well as events that have the specific type `event: message`.
 * It will not trigger on any other event type.
 */
es.addEventListener('message', (event) => {
  console.log(event.data)
})

/**
 * To explicitly close the connection, call the `close` method.
 * This will prevent any reconnection from happening.
 */
setTimeout(() => {
  es.close()
}, 10_000)

TypeScript

Make sure you have configured your TSConfig so it matches the environment you are targetting. If you are targetting browsers, this would be dom:

{
  "compilerOptions": {
    "lib": ["dom"],
  },
}

If you're using Node.js, ensure you have @types/node installed (and it is version 18 or higher). Cloudflare workers have @cloudflare/workers-types etc.

The following errors are caused by targetting an environment that does not have the necessary types available:

error TS2304: Cannot find name 'Event'.
error TS2304: Cannot find name 'EventTarget'.
error TS2304: Cannot find name 'MessageEvent'.

Migrating from v1 / v2

See MIGRATION.md for a detailed migration guide.

Extensions to the WhatWG/W3C API

Message and code properties on errors

The error event has a message and code property that can be used to get more information about the error. In the specification, the Event

es.addEventListener('error', (err) => {
  if (err.code === 401 || err.code === 403) {
    console.log('not authorized')
  }
})

Specify fetch implementation

The EventSource constructor accepts an optional fetch property in the second argument that can be used to specify the fetch implementation to use.

This can be useful in environments where the global fetch function is not available - but it can also be used to alter the request/response behaviour.

Setting HTTP request headers

const es = new EventSource('https://my-server.com/sse', {
  fetch: (input, init) =>
    fetch(input, {
      ...init,
      headers: {
        ...init.headers,
        Authorization: 'Bearer myToken',
      },
    }),
})

HTTP/HTTPS proxy

Use a package like node-fetch-native to add proxy support, either through environment variables or explicit configuration.

// npm install node-fetch-native --save
import {fetch} from 'node-fetch-native/proxy'

const es = new EventSource('https://my-server.com/sse', {
  fetch: (input, init) => fetch(input, init),
})

Allow unauthorized HTTPS requests

Use a package like undici for more control of fetch options through the use of an Agent.

// npm install undici --save
import {fetch, Agent} from 'undici'

await fetch('https://my-server.com/sse', {
  dispatcher: new Agent({
    connect: {
      rejectUnauthorized: false,
    },
  }),
})

License

MIT-licensed. See LICENSE.

版本与发布记录

@c4312/eventsource-umd 共发布 1 个版本,首次发布于 2025/03/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.5latest2025/03/151.7 年前

关于 @c4312/eventsource-umd 的常见问题

@c4312/eventsource-umd 是什么?

WhatWG/W3C compliant EventSource client for Node.js and browsers

如何安装 @c4312/eventsource-umd?

在项目目录运行 npm install @c4312/eventsource-umd,或使用 pnpm add / yarn add / bun add。

@c4312/eventsource-umd 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@c4312/eventsource-umd 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@c4312/eventsource-umd 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@c4312/eventsource-umd 可以商用吗?

@c4312/eventsource-umd 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@c4312/eventsource-umd 的下载量有多少?

根据 npm 官方下载统计,@c4312/eventsource-umd 在 2026/10/09 – 2026/11/07 这 30 天内被下载 68,480 次,日均约 2,283 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@c4312/eventsource-umd 还在维护吗?

最新版本 3.0.5 发布于 2025/03/15(1.7 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/03/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。