0

I'm using PubNub. Basically there is no problem doing publish and subscribe. However, publish does not work on slow connection like 3G network. Wifi is OK but Some people say 4G has also have same problem. Does anyone know how to fix it?

What I want to do is below.

1. UserA opens a web page from PC and subscribe a channel.
2. UserA publishes data to the channel from mobile app.
Craig Conover
  • 4,710
  • 34
  • 59
zono
  • 8,366
  • 21
  • 75
  • 113

1 Answers1

3

PubNub on 3G/4G/LTE Mobile Networks

Use the backfill: true option when you invoke the pubnub.subscribe(...) method. This allows your device to receive messages on mobile networks. The backfill option prevents mobile network latency race conditions.

// Setup
var channel = 'a'+Math.random();
var pubnub  = PUBNUB({
    ssl             : true
,   "publish_key"   : "pub-c-aefb421c-b30a-4afc-bae4-b866c5ea3d69"
,   "subscribe_key" : "sub-c-76f89e66-c3a9-11e5-b5a8-0693d8625082"
});

// Receive Message
pubnub.subscribe({
    backfill : true
,   channel  : channel
,   error    : out
,   connect  : publish
,   message  : out
});

// Send Message
function publish() { 
    out("CONNECTED!");
    pubnub.publish({
        channel : channel
    ,   error   : out
    ,   message : "SUCCESS IT WORKS!"
    });
}

// Network Check
pubnub.time(function(a){out(a ? "NETWORK CHECK" : "NETWORK BAD")});

function out(m) {
    document.getElementById("result").innerHTML 
        += "<br>" + JSON.stringify(m); 
}
<script src="https://cdn.pubnub.com/pubnub-dev.js"></script>
<h1>PubNub 3G/4G/LTE Network</h1>
<div id="result">PROCESSING_CONNECTIVITY...</div>

PubNub Mobile Network 3G 4G LTE

PubNub Mobile Network - Example Output

You may also decide that you want another option. You can also issue the pubnub.publish() method call inside the connect callback.

pubnub.subscribe({
    channel  : channel,
    message  : out,
    connect  : function() {
        pubnub.publish({
            channel : channel
        ,   message : "SUCCESS IT WORKS!"
        });
    }
});
Stephen Blum
  • 6,498
  • 2
  • 34
  • 46
  • Thank you so much. It worked!! But some people say it works but not stable (he said it worked only one in every 2 times) If PubNub has any solutions about this. I would like to know that. – zono Jan 27 '16 at 18:52
  • I'll add in the code that shows if the user has a broken network connection. – Stephen Blum Jan 27 '16 at 19:50
  • 1
    I updated the code to include a **`log trail`** and display errors if there are any. – Stephen Blum Jan 27 '16 at 19:54
  • 1
    Thank you. I will implement it in mobile app. Looks very useful because I didn't know how to get publish error log. I will put the 'error' option and then implement retry publish if error occurs. – zono Jan 27 '16 at 20:12