Socket
Socket
Sign inDemoInstall

fastify-sse-v2

Package Overview
Dependencies
Maintainers
1
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.


Version published
Weekly downloads
11K
increased by17.59%
Maintainers
1
Weekly downloads
 
Created
Source

Fastify SSE Plugin

CI check npm version

Fastify plugin for sending Server-sent events.

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 events from EventEmmiters
import {FastifySSEPlugin} from "fastify-sse-v2";
import EventIterator from "event-iterator";

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

server.get("/", function (req, res) {
    const eventEmitter = new EventEmitter();
    res.sse(new EventIterator(
                (push) => {
                  eventEmitter.on("some_event", push)
                  return () => eventEmitter.removeEventListener("some_event", push)
                }
        )
    );
});
Note
  • to remove event listeners (or some other cleanup) when client closes connection, you can listen on fastify's req.raw connection close event:

FAQs

Package last updated on 22 Jul 2020

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