Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-sse-v2

Package Overview
Dependencies
Maintainers
0
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-sse-v2

Fastify plugin for sending server side events.

  • 4.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12K
increased by4.58%
Maintainers
0
Weekly downloads
 
Created
Source

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
})
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 .

FAQs

Package last updated on 16 Sep 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc