Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding here_now and presence features, with unit tests using nodeunit…

…, also added a hook in subscribe function to exit the recursive call if required
  • Loading branch information...
commit ff34e50e587f06913432192cd584f28a8bd1e96f 1 parent 76dfb09
devendram authored August 20, 2012
70  nodejs/3.2/pubnub.js
@@ -37,6 +37,7 @@ var NOW    = 1
37 37
 ,   http   = require('http')
38 38
 ,   https  = require('https')
39 39
 ,   URLBIT = '/'
  40
+,   QUERYBIT = '&'
40 41
 ,   XHRTME = 310000
41 42
 ,   XORIGN = 1;
42 43
 
@@ -112,7 +113,9 @@ function xdr( setup ) {
112 113
             failed = 1;
113 114
             (setup.fail||function(){})(e);
114 115
         };
115  
-
  116
+    if (setup.query && setup.query.length > 0) {
  117
+        url = url + '?' + setup.query.join(QUERYBIT);
  118
+    }
116 119
     try {
117 120
         (ssl ? https : http).get( {
118 121
             host : origin,
@@ -150,7 +153,18 @@ exports.init = function(setup) {
150 153
     ,   SSL           = setup.ssl
151 154
     ,   ORIGIN        = setup.origin || 'pubsub.pubnub.com'
152 155
     ,   CHANNELS      = {}
  156
+    ,   UUID =   'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  157
+                    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
  158
+                    return v.toString(16);
  159
+                })
153 160
     ,   PN            = {
  161
+        
  162
+
  163
+        uuid : uuid = function(callback) {
  164
+            if (callback)
  165
+                callback(UUID);
  166
+            return UUID;
  167
+        },
154 168
         /*
155 169
             PUBNUB.history({
156 170
                 channel  : 'my_chat_channel',
@@ -250,7 +264,7 @@ exports.init = function(setup) {
250 264
                 callback : function(message) { console.log(message) }
251 265
             });
252 266
         */
253  
-        'subscribe' : function( args, callback ) {
  267
+        'subscribe' : subscribe = function( args, callback ) {
254 268
 
255 269
             var channel      = args['channel']
256 270
             ,   callback     = callback || args['callback']
@@ -289,6 +303,9 @@ exports.init = function(setup) {
289 303
                         encode(channel),
290 304
                         '0', timetoken
291 305
                     ],
  306
+                    query    : [
  307
+                        'uuid=' + encode(uuid())
  308
+                    ],
292 309
                     fail : function() {
293 310
                         // Disconnect
294 311
                         if (!disconnected) {
@@ -301,27 +318,27 @@ exports.init = function(setup) {
301 318
                         });
302 319
                     },
303 320
                     success : function(messages) {
304  
-                        var stop = false;
  321
+                        var ret = {stop: false};
305 322
                         if (!CHANNELS[channel].connected) return;
306 323
 
307 324
                         // Connect
308 325
                         if (!connected) {
309 326
                             connected = 1;
310  
-                            connect();
  327
+                            ret = connect();
311 328
                         }
312 329
 
313 330
                         // Reconnect
314 331
                         if (disconnected) {
315 332
                             disconnected = 0;
316  
-                            reconnect();
  333
+                            ret = reconnect();
317 334
                         }
318 335
 
319 336
                         messages[0].forEach(function(msg) {
320  
-                            stop = callback( msg, messages );
  337
+                            ret = callback( msg, messages );
321 338
                         });
322 339
 
323 340
                         timetoken = messages[1];
324  
-                        if(!stop)
  341
+                        if(!ret.stop)
325 342
                             timeout( pubnub, 10 );
326 343
                     }
327 344
                 });
@@ -330,8 +347,45 @@ exports.init = function(setup) {
330 347
             // Begin Recursive Subscribe
331 348
             pubnub();
332 349
         },
333  
-    };
  350
+        /*
  351
+            PUBNUB.presence({
  352
+                channel  : 'my_chat'
  353
+                callback : function(message) { console.log(message) }
  354
+            });
  355
+        */
  356
+        'presence' : function( args, callback ) {
  357
+            args['channel'] = args['channel'] + '-pnpres';
  358
+            subscribe(args,callback );
  359
+        },
  360
+        /*
  361
+            PUBNUB.here_now({
  362
+                channel  : 'my_chat_channel',
  363
+                callback : function(messages) { console.log(messages) }
  364
+            });
  365
+        */
  366
+        'here_now' : function( args, callback ) {
  367
+            var callback = args['callback']
  368
+            ,   channel  = args['channel'];
334 369
 
  370
+            // Make sure we have a Channel
  371
+            if (!channel)  return log('Missing Channel');
  372
+            if (!callback) return log('Missing Callback');
  373
+
  374
+            // Send Message
  375
+            xdr({
  376
+                ssl : SSL,
  377
+                url : [
  378
+                    'v2',
  379
+                    'presence',
  380
+                    'sub-key', SUBSCRIBE_KEY, 
  381
+                    'channel', encode(channel),
  382
+                ],
  383
+                origin  : ORIGIN,
  384
+                success : callback,
  385
+                fail    : function(response) { log(response) }
  386
+            });
  387
+        },
  388
+    };
335 389
     return PN;
336 390
 }
337 391
 exports.unique = unique
72  nodejs/3.2/tests/unit-test.js
... ...
@@ -1,4 +1,4 @@
1  
-var PUBNUB, channel, history_test, nodeunit, publish_dummy, publish_test, pubnub, subscribe_test, time_test, uuid_test;
  1
+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;
2 2
 
3 3
 PUBNUB = require('../pubnub');
4 4
 
@@ -43,7 +43,7 @@ time_test = function(test) {
43 43
 
44 44
 uuid_test = function(test) {
45 45
   test.expect(1);
46  
-  return pubnub.time(function(uuid) {
  46
+  return pubnub.uuid(function(uuid) {
47 47
     test.ok(uuid);
48 48
     test.done();
49 49
   });
@@ -51,7 +51,6 @@ uuid_test = function(test) {
51 51
 
52 52
 history_test = function(test) {
53 53
   test.expect(2);
54  
-  console.log('history test');
55 54
   return pubnub.history({
56 55
     limit: 1,
57 56
     channel: channel,
@@ -72,14 +71,69 @@ subscribe_test = function(test) {
72 71
     connect: function() {
73 72
       return publish_dummy(test_channel);
74 73
     },
75  
-    message: {
76  
-      test: "test"
77  
-    },
78 74
     callback: function(message) {
79 75
       test.ok(message);
80 76
       test.ok(message.test === "test");
81 77
       test.done();
82  
-      return true;
  78
+      return {stop: true};
  79
+    }
  80
+  });
  81
+};
  82
+
  83
+run_dummy_subscribe = function(channel) {
  84
+  var pubnub = PUBNUB.init({
  85
+    publish_key: 'demo',
  86
+    subscribe_key: 'demo'
  87
+  });
  88
+  return pubnub.subscribe({
  89
+    channel: channel,
  90
+    connect: function() {
  91
+      return {stop: true};
  92
+    },
  93
+    callback: function() {
  94
+      return {stop: true};
  95
+    }
  96
+  });
  97
+};
  98
+
  99
+presence_test = function(test) {
  100
+  var test_channel;
  101
+  test_channel = 'channel-' + PUBNUB.unique();
  102
+  test.expect(3);
  103
+  return pubnub.presence({
  104
+    channel: test_channel,
  105
+    connect: function() {
  106
+      run_dummy_subscribe(test_channel);
  107
+    },
  108
+    callback: function(message) {
  109
+      test.ok(message);
  110
+      test.ok(message.action === "join");
  111
+      test.ok(message.occupancy === 1);
  112
+      test.done();
  113
+      return {stop: true};
  114
+    }
  115
+  });
  116
+};
  117
+
  118
+here_now_test = function(test) {
  119
+  var test_channel;
  120
+  test_channel = 'channel-' + PUBNUB.unique();
  121
+  test.expect(2);
  122
+  return pubnub.subscribe({
  123
+    channel: test_channel,
  124
+    connect: function() {
  125
+      pubnub.here_now({
  126
+        channel: test_channel,
  127
+        callback: function(message) {
  128
+          test.ok(message);
  129
+          test.ok(message.occupancy === 1);
  130
+          publish_dummy(test_channel);
  131
+        }
  132
+      });
  133
+    },
  134
+    callback: function(message) {
  135
+      test.done();
  136
+      return {stop: true};
83 137
     }
84 138
   });
85 139
 };
@@ -89,5 +143,7 @@ module.exports = {
89 143
   "History Test": history_test,
90 144
   "Time Test": time_test,
91 145
   "UUID Test": uuid_test,
92  
-  "Subscribe Test": subscribe_test
  146
+  "Subscribe Test": subscribe_test,
  147
+  "Presence Test": presence_test,
  148
+  "Here Now Test": here_now_test
93 149
 };

0 notes on commit ff34e50

Please sign in to comment.
Something went wrong with that request. Please try again.