npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sly-fastify-sse

v0.1.6

Published

Slytrunk Fork - Provide Server-Sent Events to Fastify

Downloads

4

Readme

fastify-sse

Build Status Coverage Status Known Vulnerabilities

Easily send Server-Send-Events with Fastify.

This is based on github.com/mtharrison/susie

Install

npm install --save fastify-sse

Usage

Add it to you project with register and you are done!

You can now configure a new route, and call the new reply.sse() to send Events to browser.

When you have finished sending event, you could send an empty message, or if using stream and end of stream, an end event will be fired just before closing the connection. You could work with it browser side to prevent automatic reconnection.

// Register the plugin
fastify.register(require("fastify-sse"), (err) => {
    if (err) {
      throw err;
    }
});

// Define a new route in hapijs notation
fastify.route({
  method: "GET",
  url: "/sse-hapi",
  handler: (request, reply) => {
    let index;
    const options = {};

    // Send the first data
    reply.sse("sample data", options);
    
    // Send a new data every seconds for 10 seconds then close
    const interval = setInterval(() => {
      reply.sse({event: "test", data: index});
      if (!(index % 10)) {
        reply.sse();
        clearInterval(interval);
      }
    }, 1000);
  }
});

// Define a new route in express notation
fastify.get("/sse-express",(request, reply) => {
    let index;
    const options = {};

    // Send the first data
    reply.sse("sample data", options);
    
    // Send a new data every seconds for 10 seconds then close
    const interval = setInterval(() => {
      reply.sse({event: "test", data: index});
      if (!(index % 10)) {
        reply.sse();
        clearInterval(interval);
      }
    }, 1000);
  });

The options are used only for the first call, subsequent ignore it.

You could specify:

  • strings that well be sent directly
  • buffers that will be converted beck to strings, utf8 encoded
  • objects that will be stringified with the use of "fast-safe-stringify"
  • streams that are readables, and could deals with objectMode or not

Options

  • idGenerator: generate the event id, defaulting to a number incrementing, from 1
  • event: can be a string for the event name, or a function to compute the event name

idGenerator

It must be a function that will be called with the event in parameter, and must return a string that will be the id of the SSE, or it could be null if no id is needed.

Using a function:

reply.sse("message", {
  idGenerator: (event) => {
    // Retrieve the event name using the key myIdentifiant or use the timestamp if not exists …
    return event.myIdentifiant || (new Date()).getTime();
  }
});

It will transmit:

id: 1504624133267

data: message

Do not display id, so pass null:

reply.sse("message", {idGenerator: null});

It will transmit:

data: message

event

It could be:

  • a function, called with the event in parameter and return a string that will be used, or a string if the event name doest not change. The event will be retrieved by the browser using .on("eventName", […]).
  • nothing if you do not want a name, and events could be retrieved in the browser with the generic .on("message", […]).
reply.sse({myEventName: "myEvent", hello: "world"}, {
  event: (event) => {
    // Retrieve the event name using the key myEventName …
    const name = event.myEventName;
    // … delete it from the object …
    delete event.myEventName;
    // then return the name
    return name;
  }
});

It will transmit:

id: 1

event: MyEvent

data: {"hello":"world"}