JerryFox
JerryFox

Reputation: 625

Nodejs server unable to detect connection with Pubnub+SocketIO

My nodejs server is unable to detect when a new browser connects ('connection' event) and I dont know why. I narrowed down a problem working on it for a few days and suspect that is has to due with the addition of the pubnub socket connection implemented on the browser.

The following is my server.js

var http    = require('http')
  , connect = require('connect')
  , io      = require('socket.io')
  , fs      = require('fs')
  , uuid    = require('node-uuid')
  , _       = require('lodash');

// pubnub!!! (how to initialize it for use on server)
var pubnub  = require('pubnub').init({
   channel:        "my_channel",
   publish_key:    "pub-key",
   subscribe_key:  "sub-c-key",
   uuid:           "Server",
   origin : 'pubsub.pubnub.com'
});  

pubnub.subscribe({
    channel: 'my_channel',
    callback: function(message) {
      console.log("Message received: ", message);
    },
    message:  'Server ready',
    presence: function(data) {
      console.log("Presense: ", data);
    },
    connect:   publish
});

// various socket.on() events omitted

var app = connect().use(connect.static(__dirname)).use(connect.directory(__dirname));
var server = http.createServer(app);
server.listen(8888);
io = io.listen(server);

io.sockets.on('connection', handleNewPeer);

Upon arriving on the html page, the doConnect(isBroadcaster) function is ran from script tag

The doConnect function (In peer.js):

var doConnect = function(isBroadcaster) {

  console.log("doConnect");
  // broadcaster or normal peer
  var user;
  if (isBroadcaster) 
    user = "Broadcaster";
  else 
    user = "Viewer";

  (function() {
  var pubnub_setup = {
    channel:       "my_channel",
    publish_key:   "pub-c-key",
    subscribe_key: "sub-c-key",
    user:          user
  };

  // Note removed the var
  socket = io.connect( 'http://pubsub.pubnub.com', pubnub_setup);

 // various socket.on() omitted

 })();

Here is what how it was before with just socketIO & it was working:

var doConnect = function(isBroadcaster) {
  socket = io.connect();
// various socket.on() omitted 
}

My p2p video website is implemented with WebRTC running on a Nodejs + SocketIO server. I have been trying to incorporate pubnub into it & thought it would be easy since pubnub supports SocketIO (or at least client side?). Really did not think it would be this difficult to set up server side.

Any input at all on this? I think it's something simple that I just cannot put my finger on

Upvotes: 1

Views: 602

Answers (2)

Stephen Blum
Stephen Blum

Reputation: 6834

Socket.IO on the Server using Node.JS

Socket.IO with PubNub does not provide a Node.JS Socket.IO backend option. However you can use the PubNub SDK directly for on-connect events.

NPM Package

npm install pubnub

After you install the PubNub NPM you can use the node.js server backend:

Node.js Backend Code

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// PubNub!!! (how to initialize it for use on server)
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
var pubnub = require('pubnub').init({
   publish_key   : "pub-key",
   subscribe_key : "sub-c-key",
   uuid          : "Server-ID"
});  

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// On user Connect
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function on_user_connect(data) {
  console.log( "User Connected: ", data.uuid );
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// On user Leave
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
function on_user_leave(data) {
  console.log( "User Left: ", data.uuid );
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Open Socket Connection for User Join Events
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
pubnub.subscribe({
    channel  : 'my_channel',
    connect  :   connected
    message  : function(message) {
      console.log("Message received: ", message);
    },
    presence : function(data) {
      if (data.action == "leave")   on_user_leave(data);
      if (data.action == "timeout") on_user_leave(data);
      if (data.action == "join")    on_user_connect(data);
    }
});

function connected() {
  console.log('connected!');
}

Upvotes: 1

Schoening
Schoening

Reputation: 79

What version of socket.io are you using?

This might not fix it. I am using version 1.+ Have you tried:

    io.on('connection', function(socket){
        console.log('user connected');
    });

Upvotes: 1

Related Questions