diff --git a/README.md b/README.md index 10cd4dc..6633d48 100644 --- a/README.md +++ b/README.md @@ -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** diff --git a/examples/basic/server.js b/examples/basic/server.js index c26b8da..7974960 100644 --- a/examples/basic/server.js +++ b/examples/basic/server.js @@ -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) => { diff --git a/index.js b/index.js index 463e58e..6d20c21 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ var longpoll = function(app, opts) { // Default Config var config = { DEBUG: false, + subscriberCallback: null, events: { maxListeners: 0 // unlimited } @@ -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 @@ -140,6 +141,11 @@ var longpoll = function(app, opts) { // Create it sub(res); + + // New subscriber callback + if (config.subscriberCallback){ + config.subscriberCallback(req); + }; }); resolve(); }); diff --git a/package.json b/package.json index 4ab661c..cdb0998 100644 --- a/package.json +++ b/package.json @@ -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",