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

sse-pusher

v1.1.1

Published

Simple server-sent events (SSE) for Connect and Express

Downloads

481

Readme

SSE-Pusher

npm version Build Status Coverage Status Dependency Status

Simple server-sent events (SSE) for Connect and Express.

Installation

$ npm install --save sse-pusher

API

var ssePusher = require('sse-pusher'); 

var push = ssePusher(); // instantiation variant 1
var push = ssePusher.create(); // instantiation variant 2

push([event,] data)

Pushes an optionally typed (i.e., using the event parameter) event to all connected SSE clients.

Parameters:

  • event - event type, must be a string;
  • data - event data, can be anything that can be serialized using JSON.stringify(). More precisely, anything that is not a string, number or boolean will be serialized using JSON.stringify().

push.handler([mountPath])

Returns a function that can be used both as a Connect/Express middleware and an Express route handler.

Parameters:

  • mountPath - path where the Connect/Express middleware shall be mounted (e.g., /some/path).

Usage

Server-side

First, you have to load the package a instantiate a new SSE-Pusher:

// load package:
var ssePusher = require('sse-pusher'); 

// instantiate a new SSE-Pusher:
var push = ssePusher();

Afterwards, you have to "wire" the SSE-Pusher with you HTTP framework of choice (i.e., Connect or Express):

var app = connect() || express();

// install the pusher as a Connect/Express middleware:
app.use(push.handler('/some/path')); // variant 1
app.use('/some/path', push.handler()); // variant 2

// install the pusher as an Express route handler:
app.get('/some/path', push.handler());

Finally, using push(event, data) or push(data) you can then start pushing data to connected SSE clients:

// push some data:
push('eventname', 'eventdata');

// push some data without specifying an event name:
push({some: 'data'});

Client-side

On the client (i.e., the Web browser) you may then listen to the server-side emitted messages using the following code:

var es = new EventSource('http://localhost:3000/some/path');

// when using push('hello') on the server:
es.onmessage = function (event) {
  console.log(event.data); // logs 'hello'
};

// when using push('greeting', 'world') on the server:
es.addEventListener('greeting', function (event) {
  console.log(event.data); // logs 'world'
});