fastify-sse-v2

Fastify plugin for sending server side events.

MIT 25 个版本
安装
npm install fastify-sse-v2
yarn add fastify-sse-v2
pnpm add fastify-sse-v2
bun add fastify-sse-v2
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)
版本列表
4.2.2 2026-01-27
4.2.1 2024-12-05
4.2.0 2024-12-02
4.1.0 2024-09-16
4.0.0 2024-03-20
3.1.2 2023-07-25
3.1.1 2023-07-03
3.1.0 2023-04-03
3.0.0 2022-11-22
2.2.1 2022-06-09
2.2.0 2022-06-06
2.0.6 2021-07-05
2.0.4 2021-04-12
2.0.3 2021-01-18
2.0.2 2020-07-23
2.0.1 2020-07-22
2.0.0 2020-07-20
1.0.7 2020-07-23
1.0.6 2020-07-22
1.0.5 2020-07-20
1.0.4 2020-06-05
1.0.3 2020-04-09
1.0.2 2020-04-09
1.0.1 2020-04-09
1.0.0 2020-04-09