Coder Social home page Coder Social logo

fastify-sse-v2's Introduction

Fastify SSE Plugin

CI check npm version

Fastify plugin for sending Server-sent events.

For [email protected] use [email protected]!

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())
})
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());

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.