Navigation Menu

Skip to content

Commit

Permalink
Adding here_now and presence features, with unit tests using nodeunit…
Browse files Browse the repository at this point in the history
…, also added a hook in subscribe function to exit the recursive call if required
  • Loading branch information
Devendra committed Aug 19, 2012
1 parent 76dfb09 commit ff34e50
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 16 deletions.
70 changes: 62 additions & 8 deletions nodejs/3.2/pubnub.js
Expand Up @@ -37,6 +37,7 @@ var NOW = 1
, http = require('http')
, https = require('https')
, URLBIT = '/'
, QUERYBIT = '&'
, XHRTME = 310000
, XORIGN = 1;

Expand Down Expand Up @@ -112,7 +113,9 @@ function xdr( setup ) {
failed = 1;
(setup.fail||function(){})(e);
};

if (setup.query && setup.query.length > 0) {
url = url + '?' + setup.query.join(QUERYBIT);
}
try {
(ssl ? https : http).get( {
host : origin,
Expand Down Expand Up @@ -150,7 +153,18 @@ exports.init = function(setup) {
, SSL = setup.ssl
, ORIGIN = setup.origin || 'pubsub.pubnub.com'
, CHANNELS = {}
, UUID = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
})
, PN = {


uuid : uuid = function(callback) {
if (callback)
callback(UUID);
return UUID;
},
/*
PUBNUB.history({
channel : 'my_chat_channel',
Expand Down Expand Up @@ -250,7 +264,7 @@ exports.init = function(setup) {
callback : function(message) { console.log(message) }
});
*/
'subscribe' : function( args, callback ) {
'subscribe' : subscribe = function( args, callback ) {

var channel = args['channel']
, callback = callback || args['callback']
Expand Down Expand Up @@ -289,6 +303,9 @@ exports.init = function(setup) {
encode(channel),
'0', timetoken
],
query : [
'uuid=' + encode(uuid())
],
fail : function() {
// Disconnect
if (!disconnected) {
Expand All @@ -301,27 +318,27 @@ exports.init = function(setup) {
});
},
success : function(messages) {
var stop = false;
var ret = {stop: false};
if (!CHANNELS[channel].connected) return;

// Connect
if (!connected) {
connected = 1;
connect();
ret = connect();
}

// Reconnect
if (disconnected) {
disconnected = 0;
reconnect();
ret = reconnect();
}

messages[0].forEach(function(msg) {
stop = callback( msg, messages );
ret = callback( msg, messages );
});

timetoken = messages[1];
if(!stop)
if(!ret.stop)
timeout( pubnub, 10 );
}
});
Expand All @@ -330,8 +347,45 @@ exports.init = function(setup) {
// Begin Recursive Subscribe
pubnub();
},
};
/*
PUBNUB.presence({
channel : 'my_chat'
callback : function(message) { console.log(message) }
});
*/
'presence' : function( args, callback ) {
args['channel'] = args['channel'] + '-pnpres';
subscribe(args,callback );
},
/*
PUBNUB.here_now({
channel : 'my_chat_channel',
callback : function(messages) { console.log(messages) }
});
*/
'here_now' : function( args, callback ) {
var callback = args['callback']
, channel = args['channel'];

// Make sure we have a Channel
if (!channel) return log('Missing Channel');
if (!callback) return log('Missing Callback');

// Send Message
xdr({
ssl : SSL,
url : [
'v2',
'presence',
'sub-key', SUBSCRIBE_KEY,
'channel', encode(channel),
],
origin : ORIGIN,
success : callback,
fail : function(response) { log(response) }
});
},
};
return PN;
}
exports.unique = unique
72 changes: 64 additions & 8 deletions nodejs/3.2/tests/unit-test.js
@@ -1,4 +1,4 @@
var PUBNUB, channel, history_test, nodeunit, publish_dummy, publish_test, pubnub, subscribe_test, time_test, uuid_test;
var PUBNUB, channel, here_now_test, history_test, nodeunit, presence_test, publish_dummy, publish_test, pubnub, run_dummy_subscribe, subscribe_test, time_test, uuid_test;

PUBNUB = require('../pubnub');

Expand Down Expand Up @@ -43,15 +43,14 @@ time_test = function(test) {

uuid_test = function(test) {
test.expect(1);
return pubnub.time(function(uuid) {
return pubnub.uuid(function(uuid) {
test.ok(uuid);
test.done();
});
};

history_test = function(test) {
test.expect(2);
console.log('history test');
return pubnub.history({
limit: 1,
channel: channel,
Expand All @@ -72,14 +71,69 @@ subscribe_test = function(test) {
connect: function() {
return publish_dummy(test_channel);
},
message: {
test: "test"
},
callback: function(message) {
test.ok(message);
test.ok(message.test === "test");
test.done();
return true;
return {stop: true};
}
});
};

run_dummy_subscribe = function(channel) {
var pubnub = PUBNUB.init({
publish_key: 'demo',
subscribe_key: 'demo'
});
return pubnub.subscribe({
channel: channel,
connect: function() {
return {stop: true};
},
callback: function() {
return {stop: true};
}
});
};

presence_test = function(test) {
var test_channel;
test_channel = 'channel-' + PUBNUB.unique();
test.expect(3);
return pubnub.presence({
channel: test_channel,
connect: function() {
run_dummy_subscribe(test_channel);
},
callback: function(message) {
test.ok(message);
test.ok(message.action === "join");
test.ok(message.occupancy === 1);
test.done();
return {stop: true};
}
});
};

here_now_test = function(test) {
var test_channel;
test_channel = 'channel-' + PUBNUB.unique();
test.expect(2);
return pubnub.subscribe({
channel: test_channel,
connect: function() {
pubnub.here_now({
channel: test_channel,
callback: function(message) {
test.ok(message);
test.ok(message.occupancy === 1);
publish_dummy(test_channel);
}
});
},
callback: function(message) {
test.done();
return {stop: true};
}
});
};
Expand All @@ -89,5 +143,7 @@ module.exports = {
"History Test": history_test,
"Time Test": time_test,
"UUID Test": uuid_test,
"Subscribe Test": subscribe_test
"Subscribe Test": subscribe_test,
"Presence Test": presence_test,
"Here Now Test": here_now_test
};

0 comments on commit ff34e50

Please sign in to comment.