简体   繁体   中英

Issue setting up Pubnub Nodejs server

Following the example from: https://www.npmjs.org/package/pubnub

I am trying to set up a connection from my nodejs server to pubnub via:

var pubnub  = require("pubnub").init({
    publish_key:    "pub key here",
    subscribe_key:  "sub key here",
    channel:        'my_channel',
    user:           'Server'
});  

pubnub.subscribe({
    channel: 'my_channel',
    callback: function(message) {
      console.log("Message received: ", message);
    }
});

pubnub.publish({ 
    channel   : 'my_channel',
    callback  : function(e) { console.log( "SUCCESS!", e ); },
    error     : function(e) { console.log( "FAILED! RETRY PUBLISH!", e ); }
});

// test msg to see if server connect to pubnub channel
function publish() {
  pubnub.publish({ 
    channel   : 'my_channel',
    message   : 'Server subscribed'
  });
}

I thought this followed the example provided but my server will not show up in the pubnub log nor receive any SocketIO events from the 2 peers trying to connect to each other. I am trying to use pubnub with SocketIO to send over ice candidates to establish p2p video via WebRTC

For peers that are trying to connect that are actually do have a pubnub connection working, the set up is as follows:

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

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

// various socket.on( ... )

})();

@jerryfox, there is a connection callback on subscribe called "connect". You should only issue your test publish on this "connect" callback... otherwise, you can publish before you've actually connected, and you'll never see the message. Try this:

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

If you are still having issues, contact us at support@pubnub.com, and we'll help resolve the issue for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM