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

dumb

v0.1.2

Published

Allows non-evented apps to subscribe to Node.js events over HTTP

Downloads

13

Readme

DumbPubSub

This module is for Node.js applications in a heterogeneus environment, where you want your non long-running-process applications (such as a PHP app) to be able to subscribe to events from your Node.js application.

For example, if a client updates their email address, and you want to alert your PHP application so that it can perhaps change some memcache entry it uses, this module is what you are looking for.

External apps can provide an event to bind on to, and a URL to listen on. When the event occurs, the relevant data is POSTed to said URL.

Long term goals for this project include saving the subscription data to either a Redis or MongoDB database. The first iteration will only save it to a local JSON file. There may eventually be so many entries that keeping it in Node memory wouldn't work, so will have to explore that.

Also, you wouldn't want to use this for inter-nodejs-app communications, for that you would want to use someone elses library.

Example Requests

POST /subscribe
P:event
P:url
Success: 201 CREATED
Failure: 409 CONFLICT

DELETE /subscribe
(P/G:event)
(P/G:url)
Success: 200 OK
Failure: 404 NOT FOUND

GET /subscribe
(G:event)
(G:url)
Success: 200 OK

Example Server Code

var dumb = require('dumbpubsub');
// npm install express
var express = require('express');

var app = express(); // Create an Express app
app.use(express.bodyParser());

dumb.attach(app) // DumbPubSub will now use the existing Express app
    .notifyEvent() // By default, we don't tell client what was run, we assume their URL will let them know
    .persistOnExit() // Makes sure we save all subscriptions to disk when we quit
    .restore('subscriptions.json') // Reads this file for subscription info
    .setUrl('/subscribe') // Sets the URL which we listen on
    .enable(); // Tells the express app that we want to listen on some URLs

// Normal application requests work as expected
app.get('/', function(req, res) {
    res.send('hello world');
});

// Allows us to trigger an event using a browser, for testing purposes
app.get('/trigger/:event', function(req, res) {
    dumb.emit(req.route.params.event, {
        from: 'web'
    });
    res.send(200);
});

app.listen(3000); // Express (and DumbPubSub) both listen on the same port
console.log('Listening for incomming HTTP requests.');

// Emit an event after 10 seconds
setTimeout(function() {
    dumb.emit('client-update', {
        clientId: 230948230,
        oldEmail: '[email protected]',
        newEmail: '[email protected]'
    });
}, 10000);

Example subscriptions.json

[
    {
        "event": "client-update",
        "url": "http://localhost/listener.php
    }
]

Debugging

Here's a tool for Chrome that allows you to make various HTTP requests:

https://chrome.google.com/webstore/detail/hgmloofddffdnphfgcellkdfbfbjeloo