Search code examples
javascriptnode.jspubnub

Asynchronous publish and subscribe messages is not working in pubnub


I am using the following code for pubnub connection.

pubnub = new PubNub({
    subscribeKey: "demo",
    publishKey: "demo",
        keepAlive: true,
})
pubnub.addListener({
    message: function(message){
        console.log(message.channel+' '+message.publisher+' '+message.message.name+' '+time)
},
    status: function(s) {
        console.log(s.category+" "+s.operation+" "+s.affectedChannels +" "+s.subscribedChannels +" "+s.affectedChannelGroups+" "+time+" "+s.message)
    }
 });
 pubnub.history(
 {
    //child0, new
    channel: 'child3',
    reverse: true,
    count: 10
 },
 function (status, response) {
    // console.log(response);
    console.log(response.messages);
 }
 );
 pubnub.subscribe({
     channels: ['child3'],
     // resubscribe and reconnect
     restore : true, 
     //callback: function(m){console.log(m)} ,
 });

I had two nodejs scripts, one for publish and another for subscribe a message in pubnub.

When the Subscriber is online, it is able to receive messages from the Publisher but when the Subscriber goes offline, it is unable to receive the appropriate message from the Publisher due to a faulty network connection, occurring due to an erroneous Subscribe Script.

Please resolve this issue.


Solution

  • PubNub Subscribe Online vs Offline

    When the Subscriber is online, it is able to receive messages from the Publisher but when the Subscriber goes offline, it is unable to receive the appropriate message from the Publisher due to a faulty network connection, occurring due to an erroneous Subscribe Script.

    This is completely expected. You say erroneous subscribe script but there is nothing erroneous about it. Nothing is going to work unless you are connected. Once your app is offline, how would you expect the app to receive any messages? So there is no error here with PubNub. It working as it should.

    Get connected and you will get messages.