Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

fastify-sse-v2

nodefactoryio74.8kMIT4.2.1TypeScript support: included

Fastify plugin for sending server side events.

readme

Fastify SSE Plugin

CI check npm version

Fastify plugin for sending Server-sent events.

For `fastify@2.x` use fastify-sse-v2@1.x!

How to use?

yarn add fastify-sse-v2

Register fastify-sse-v2 plugin into your fastify instance:

import { FastifySSEPlugin } from "fastify-sse-v2";

const server = fastify();
server.register(FastifySSEPlugin);

Sending events from AsyncIterable source

import { FastifySSEPlugin } from "fastify-sse-v2";

const server = fastify();
server.register(FastifySSEPlugin);

server.get("/", function (req, res) {
  res.sse(
    (async function* source() {
      for (let i = 0; i < 10; i++) {
        sleep(2000);
        yield { id: String(i), data: "Some message" };
      }
    })()
  );
});

Sending individual events

import { FastifySSEPlugin } from "fastify-sse-v2";

const server = fastify();
server.register(FastifySSEPlugin);

server.get("/", async function (req, res) {
  for (let i = 0; i < 10; i++) {
    await sleep(2000);
    res.sse({ id: String(i), data: "Some message" });
  }
});

fastify.get("/listenForChanges", {}, (request, reply) => {
  const listenStream = fastify.db
    .watch("doc-uuid")
    .on("data", (data) => reply.sse({ data: JSON.stringify(data) }))
    .on("delete", () => reply.sse({ event: "close" }));
  request.socket.on("close", () => listenStream.end());
});
Note
  • When sending individual events, the connection is kept open until you call reply.sseContext.source.end() to terminate the stream.
Sending events from EventEmmiters
import { FastifySSEPlugin } from "fastify-sse-v2";
import { on } from "events";

const server = fastify();
server.register(FastifySSEPlugin);

server.get("/", function (req, res) {
  res.sse(
    (async function* () {
      for await (const [event] of on(eventEmmitter, "update")) {
        yield {
          event: event.name,
          data: JSON.stringify(event),
        };
      }
    })()
  );
});
Note
  • to remove event listeners (or some other cleanup) when client closes connection, you can listen on connection closing event: request.socket.on('close', () => abortController.abort());
Change server send retry behavior
import { FastifySSEPlugin } from "fastify-sse-v2";

const server = fastify();

server.register(FastifySSEPlugin) // retryDelay default 3000

server.register(FastifySSEPlugin, {
  retryDelay: false // disable retryDelay
  retryDelay: 5000 // override 5000
})
Change default highWaterMark
import { FastifySSEPlugin } from "fastify-sse-v2";

const server = fastify();

server.register(FastifySSEPlugin) // highWaterMark defaults to 16384 bytes (16kb)

server.register(FastifySSEPlugin, {
  highWaterMark: 1024 // override default setting of 16384 (16kb) with 1024 (1kb)
})
Note
  • You can set parameter retryDelay to false to disable the default behavior of sending retry, or set parameter retryDelay to milliseconds override the default 3000 retry interval .
  • You can set parameter highWaterMark to define the buffer size (in bytes) that determines when the buffer is full and a 'flush' should be performed. Default is 16kb. (Learn more)

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

4.2.1 (2024-12-05)

Bug Fixes

  • handle newlines in the data properly (#93) (f83d8d2)

4.2.0 (2024-12-02)

Features

  • add the ability to override default stream buffer size (fd0382d)

Miscellaneous

  • bump cross-spawn from 7.0.3 to 7.0.6 (#90) (2553e02)

4.1.0 (2024-09-16)

Features

Miscellaneous

  • bump braces from 3.0.2 to 3.0.3 (#82) (6bdaa62)
  • bump micromatch from 4.0.5 to 4.0.8 (#87) (866547c)
  • bump path-to-regexp from 1.8.0 to 1.9.0 (#88) (6e0f9c2)

4.0.0 (2024-03-20)

⚠ BREAKING CHANGES

  • there is no "end" event when stream is closing in case you depended on it

Bug Fixes

  • remove default stream close event (#81) (311c920)

Miscellaneous

  • bump get-func-name from 2.0.0 to 2.0.2 (#78) (3495dbc)

3.1.2 (2023-07-25)

Bug Fixes

  • edge case when user (or server) sends headers before first event is sent (#73) (77d1b51)

Miscellaneous

3.1.1 (2023-07-03)

Bug Fixes

  • add utf-8 charset to content-type (#68) (4fb15be)

Miscellaneous

3.1.0 (2023-04-03)

Features

  • Added option to send comment in Event (#60) (a11044a)

Bug Fixes

Miscellaneous

3.0.0 (2022-11-22)

⚠ BREAKING CHANGES

  • Drop support for Fastify v3

Bug Fixes

  • missing closing bracket in readme (#50) (b3496f2)

Miscellaneous

2.2.1 (2022-06-09)

Bug Fixes

2.2.0 (2022-06-06)

Features

Miscellaneous

2.1.0 (2022-06-06)

Features

Unreleased

[2.0.6]

Fixed

  • removed vulnerable and not needed dependencies

    [2.0.5]

Fixed

  • added export for EventMessage

[2.0.4]

Fixed

  • headers from reply (other plugins like cors) not being set on stream
  • vulnerable dependencies

[2.0.3]

Added

  • default export

2.0.2

Fixes

  • headers not being set

2.0.1

Bugfix

  • add headers to prevent compression

2.0.0

Feature

  • fastify v3 support

1.0.4

Fixed

  • bug with too much new lines between messages

1.0.3

Fixed

  • type definitions referencing itself

1.0.2

Fixed

  • update npm links

1.0.1

Fixed

  • update what files gets published on npm