Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new subscriber callback #6

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ var app = express();
var longpoll = require("express-longpoll")(app)
// You can also enable debug flag for debug messages
var longpollWithDebug = require("express-longpoll")(app, { DEBUG: true });
// You can also specify a function that is called when a new longpoll request is made
var longpollWithDebugAndCallback = require("express-longpoll")(app, { DEBUG: true,
subscriberCallback: function (id){
console.log (`New subscriber with id: ${id}`);
}
});
```

**Quick-start code**
Expand Down
5 changes: 4 additions & 1 deletion examples/basic/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var express = require('express');
var app = express();
const longpoll = require("../../index.js")(app, {
DEBUG: true
DEBUG: true,
subscriberCallback: function (id) {
console.log(`Subscriber Callback with id: ${id}`);
}
});

app.use((req, res, next) => {
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var longpoll = function(app, opts) {
// Default Config
var config = {
DEBUG: false,
subscriberCallback: null,
events: {
maxListeners: 0 // unlimited
}
Expand Down Expand Up @@ -125,7 +126,7 @@ var longpoll = function(app, opts) {
eventId = eventId + "." + req.id;
// Clear all previous events for the ID, we only need one
log("Old Events cleared: ", url, eventId);
dispatcher.removeAllListeners([eventId]);
dispatcher.removeAllListeners(eventId);
}

// Method that Creates event listener
Expand All @@ -140,6 +141,11 @@ var longpoll = function(app, opts) {

// Create it
sub(res);

// New subscriber callback
if (config.subscriberCallback){
config.subscriberCallback(req);
};
});
resolve();
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"dependencies": {
"bluebird": "^3.5.0",
"eventemitter2": "3.0.0",
"lodash": "4.17.4"
"lodash": ">=4.17.5"
},
"devDependencies": {
"chai": "^3.5.0",
Expand Down