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

nudge

v0.3.0

Published

Wrap a Node.js event emitter in an express middleware.

Downloads

13

Readme

nudge

Turn Node.js event emitters into Server Sent Event (aka EventSource) sources!

This module provides a function that wraps an event emitter, returning an express middleware that responds to browser EventSource requests. The wrapper can be configured to listen for particular events, optionally renaming and/or pre-processing them before forwarding to a client.

Usage

Basic

The most basic usage is to pass the function an event emitter, and an object with fields corresponding to events to listen on, and values set to true. For example:

var nudge = require('nudge');
var express = require('express');
var EventEmitter = require('events').EventEmitter;

var testEmitter = new EventEmitter();

// Listen for 'testEvent' and 'anotherTestEvent' events.
var testEmitterRelay = nudge(testEmitter, {
    testEvent: true,
    anotherTestEvent: true
});

var app = express();

// Navigate to localhost:3000.
app.get('/', function (req, res) {
    res.send(200);
});

// Host the events on a path. Use:
//     var emitter = new EventSource('/emitter');
//     emitter.addEventListener('testEvent', function (e) {
//         console.log(JSON.parse(e.data));
//     });
// To listen for 'testEvent' events and display destringified data.

app.get('/emitter', testEmitterRelay);

app.listen(3000);

// Intervals to emit sample events to send.
setInterval(function () {
    testEmitter.emit('testEvent', 'Hello, world!');
}, 1000);

setInterval(function () {
    testEmitter.emit('anotherTestEvent', 'Goodbye!');
}, 1500);

Advanced

You can rename and do some custom pre-processing on events before sending them through to the client. One important difference between EventSource and the Node.js EventEmitters is that EventSource listeners only have one data argument, whereas Node emitters may have many. Pre-processing gives you a chance to do something to the data from emitters like this in order to reduce them to a single argument. The callback of the preProcessor is used as the data argument. It's important to note that this is a success callback. Any errors should be handled by your function separately. By not calling the success callback, you can effectively filter results.

Take for example:

var testEmitterRelay = nudge(twitterEmitter, {
    testEvent: true,
    data: {
        name: 'tweet'
        preProcessor: function (args, successCallback) {
            successCallback({ user: args[0], tweet: args[1] });
        }
    }
});

Here the testEvent is just as before, but 'data' events are being renamed to 'tweet' and the two data arguments of emissions wrapped in a single object.

It's important to note that the callback passed to the pre-processor doesn't take an error. It's up to your pre-processor function to know what to do when errors occur.

Contributing

Contributions are welcome! Please see the contributing document.