0

I am trying to use socket.io with pubnub. I am not able to understand how to get data on client(index.html) which I have published on the server.Below is the sample code.

my server.js

var pubnub = require("pubnub")

var p = pubnub.init({
  "subscribe_key" : "xxxx",
 "publish_key" : "xxxx",
 "params" : {},
});

 p.publish({
  "message" : "foo",
  "channel" : "test_channel",
 });

client code - index.html

  <script src="http://cdn.pubnub.com/socket.io.min.js"></script>
  <script>(function(){
   // IMPORTANT: PubNub Setup with Account
   var pubnub_setup = {
      channel       : 'test_channel',
      publish_key   : 'xxxx',
     subscribe_key : 'xxxx'
     };

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

   socket.on( 'connect', function() {
    console.log('Connection Established! Ready to send/receive data!');
   } );

   socket.on( 'message', function(message) {
    console.log(message);
   } );

   socket.on( 'disconnect', function() {
    console.log('my connection dropped');
   } );


   socket.on( 'reconnect', function() {
    console.log('my connection has been restored!');
   } );

 })();</script>
jena84
  • 311
  • 1
  • 3
  • 20

1 Answers1

0

PubNub Socket.io SDK

As stated in this other Stack Overflow thread, the PubNub Socket.IO SDK for PubNub is designed for people that started with socket.io but want to migrate to PubNub. Otherwise, there is no requirement to use Socket.IO SDK if you are starting with PubNub first and you should use the latest PubNub Node SDK and PubNub JavaScript SDK or whatever PubNub SDK you require.

And the newly implemented, ES5 compliant PubNub JavaScript/Node v4 SDKs (currently in beta) are coming soon!

Community
  • 1
  • 1
Craig Conover
  • 4,710
  • 34
  • 59