# eventsource-encoder

> Encodes events as well-formed EventSource/Server Sent Event (SSE) messages

Latest version **1.0.2** (published 2026-04-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install eventsource-encoder
pnpm add eventsource-encoder
yarn add eventsource-encoder
bun add eventsource-encoder
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2026-04-27 |
| First published | 2024-10-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18.0.0 |
| Dependencies | 0 |
| Unpacked size | 182.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 4 |
| Author | Espen Hovlandsdal |
| Maintainers | rexxars |
| Keywords | sse, eventsource, server-sent-events |

## Links

- npm: https://www.npmjs.com/package/eventsource-encoder
- Repository: https://github.com/rexxars/eventsource-encoder
- Homepage: https://github.com/rexxars/eventsource-encoder#readme
- Issues: https://github.com/rexxars/eventsource-encoder/issues
- npm.io page: https://npm.io/package/eventsource-encoder

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2026-04-27
- 1.0.1 — 2025-02-12
- 1.0.0 — 2024-10-09

## README

# eventsource-encoder

[![npm version](https://img.shields.io/npm/v/eventsource-encoder.svg?style=flat-square)](https://www.npmjs.com/package/eventsource-encoder)[![npm bundle size](https://img.shields.io/bundlephobia/minzip/eventsource-encoder?style=flat-square)](https://bundlephobia.com/result?p=eventsource-encoder)[![npm weekly downloads](https://img.shields.io/npm/dw/eventsource-encoder.svg?style=flat-square)](https://www.npmjs.com/package/eventsource-encoder)

An encoding utility package for [server-sent events/eventsource](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events).

While the protocol is very simple in theory, there are some common pitfalls - in particular handling of newlines. This package makes it extremely simple to implement a Server-Sent Events endpoint with well-formed messages.

## Installation

```bash
npm install --save eventsource-encoder
```

## Usage

```js
import {createServer} from 'node:http'
import {encode} from 'eventsource-encoder'

createServer((req, res) => {
  res.writeHead(200, {
    'Content-Type': 'text/event-stream',
    'Cache-Control': 'no-cache',
  })

  for (let id = 0; id < 100; id++) {
    res.write(
      encode({
        id,
        event: 'time',
        data: new Date().toISOString(),
      }),
    )
  }
})
```

## Encoding data chunks only

If you want to encode only the data part of the event, you can use the `encodeData` function:

```js
import {encodeData} from 'eventsource-encoder'

console.log(encodeData('Hello, world!')) // data: Hello, world!\n\n
console.log(encodeData('Hello\nworld!')) // data: Hello\ndata: world!\n\n
```

## Encoding comments

"Comments" in Server-Sent Events are lines that start with a `:` character. You can use the `encodeComment` function to encode comments. Note that most EventSource clients have no way of interacting with comments, but they can be useful for debugging or for keeping the connection alive with "heartbeats".

```js
import {encodeComment} from 'eventsource-encoder'

console.log(encodeComment('This is a comment')) // : This is a comment\n
console.log(encodeComment('This\nis\na\nmultiline\ncomment')) // : This\n: is\n: a\n: multiline\n: comment\n
```

## License

MIT © [Espen Hovlandsdal](https://espen.codes/)

---
_Source: https://npm.io/package/eventsource-encoder · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
