Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tlhunter/dumbpubsub
Allows non-evented apps to subscribe to Node.js events over HTTP
https://github.com/tlhunter/dumbpubsub
Last synced: 25 days ago
JSON representation
Allows non-evented apps to subscribe to Node.js events over HTTP
- Host: GitHub
- URL: https://github.com/tlhunter/dumbpubsub
- Owner: tlhunter
- Created: 2012-09-01T00:07:14.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2012-09-10T20:41:00.000Z (about 12 years ago)
- Last Synced: 2024-09-13T23:15:55.958Z (about 2 months ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/dumb
- Size: 654 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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 CONFLICTDELETE /subscribe
(P/G:event)
(P/G:url)
Success: 200 OK
Failure: 404 NOT FOUNDGET /subscribe
(G:event)
(G:url)
Success: 200 OKExample Server Code
===var dumb = require('dumb');
// 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"
}
]Installation
===npm install dumb express
# copy server.js code from above
# copy subscriptions.json code from above
npm server.jsDebugging
===Here's a tool for Chrome that allows you to make various HTTP requests:
https://chrome.google.com/webstore/detail/hgmloofddffdnphfgcellkdfbfbjeloo
License
===Dual BSD/GPL