diff --git a/node.js/examples/keep-alive.js b/node.js/examples/keep-alive.js new file mode 100644 index 000000000..34706bfbc --- /dev/null +++ b/node.js/examples/keep-alive.js @@ -0,0 +1,29 @@ +// see current sockets using 'netstat -tonpc' +var Pubnub = require('../pubnub'), + conf = { + publish_key: 'demo', + subscribe_key: 'demo' + }, + i = 20, + timeout = 501, + p; + +p = Pubnub.init(conf); + +function publish(i) { + p.publish({ + channel: 'somechannel', + message: 'hey' + i, + callback: function (result) { + console.log(result); + } + }); + + if (i-- !== 0) { + setTimeout(function () { + publish(i); + }, timeout); + } +} + +publish(i); diff --git a/node.js/package.json b/node.js/package.json index 58e6a0016..47224b974 100644 --- a/node.js/package.json +++ b/node.js/package.json @@ -28,7 +28,9 @@ "real-time", "messaging" ], - "dependencies" : { }, + "dependencies" : { + "agentkeepalive": "~0.2" + }, "noAnalyze": false, "devDependencies": {}, "bundleDependencies": [], diff --git a/node.js/pubnub.js b/node.js/pubnub.js index c16c539bc..c7d810b55 100644 --- a/node.js/pubnub.js +++ b/node.js/pubnub.js @@ -1459,6 +1459,11 @@ THE SOFTWARE. var NOW = 1 , http = require('http') , https = require('https') +, keepAliveAgent = new (keepAliveIsEmbedded() ? http.Agent : require('agentkeepalive'))({ + keepAlive: true, + keepAliveMsecs: 300000, + maxSockets: 5 + }) , XHRTME = 310000 , DEF_TIMEOUT = 10000 , SECOND = 1000 @@ -1551,7 +1556,8 @@ function xdr( setup ) { options.path = proxy ? "http://" + origin + url:url; options.headers = proxy ? { 'Host': origin }:null; options.method = mode; - options.agent = false; + options.keepAlive= !!keepAliveAgent; + options.agent = keepAliveAgent; options.body = payload; require('http').globalAgent.maxSockets = Infinity; @@ -1640,6 +1646,9 @@ function crypto_obj() { } } +function keepAliveIsEmbedded() { + return 'EventEmitter' in http.Agent.super_; +} var CREATE_PUBNUB = function(setup) { @@ -1650,6 +1659,11 @@ var CREATE_PUBNUB = function(setup) { setup['hmac_SHA256'] = get_hmac_SHA256; setup['crypto_obj'] = crypto_obj(); setup['params'] = {'pnsdk' : PNSDK}; + + if (setup['keepAlive'] === false) { + keepAliveAgent = undefined; + } + SELF = function(setup) { return CREATE_PUBNUB(setup); } diff --git a/node.js/unassembled/platform.js b/node.js/unassembled/platform.js index 11a008cc4..539454832 100644 --- a/node.js/unassembled/platform.js +++ b/node.js/unassembled/platform.js @@ -35,6 +35,11 @@ THE SOFTWARE. var NOW = 1 , http = require('http') , https = require('https') +, keepAliveAgent = new (keepAliveIsEmbedded() ? http.Agent : require('agentkeepalive'))({ + keepAlive: true, + keepAliveMsecs: 300000, + maxSockets: 5 + }) , XHRTME = 310000 , DEF_TIMEOUT = 10000 , SECOND = 1000 @@ -127,7 +132,8 @@ function xdr( setup ) { options.path = proxy ? "http://" + origin + url:url; options.headers = proxy ? { 'Host': origin }:null; options.method = mode; - options.agent = false; + options.keepAlive= !!keepAliveAgent; + options.agent = keepAliveAgent; options.body = payload; require('http').globalAgent.maxSockets = Infinity; @@ -216,6 +222,9 @@ function crypto_obj() { } } +function keepAliveIsEmbedded() { + return 'EventEmitter' in http.Agent.super_; +} var CREATE_PUBNUB = function(setup) { @@ -226,6 +235,11 @@ var CREATE_PUBNUB = function(setup) { setup['hmac_SHA256'] = get_hmac_SHA256; setup['crypto_obj'] = crypto_obj(); setup['params'] = {'pnsdk' : PNSDK}; + + if (setup['keepAlive'] === false) { + keepAliveAgent = undefined; + } + SELF = function(setup) { return CREATE_PUBNUB(setup); } diff --git a/web/examples/simple.html b/web/examples/simple.html index 7027cfe89..5b42aad48 100644 --- a/web/examples/simple.html +++ b/web/examples/simple.html @@ -4,8 +4,9 @@ // INIT PubNub console.log(PUBNUB); var pubnub = PUBNUB.init({ - publish_key : 'demo', - subscribe_key : 'demo' + leave_on_unload : true, // Perform a Leave Explicitly on Page Unload Event + publish_key : 'demo', + subscribe_key : 'demo' }); pubnub.subscribe({ callback : function(r){console.log(r);},