-->

Pubnub subscribe stops receiving messages after so

2019-07-21 02:41发布

问题:

I have some remote devices which communicate with a central Meteor application running on a Digital Ocean Ubuntu 14.04 droplet in a Docker container. Each device has it's own channel, and the server is subscribed to all the channels plus a "telemetry" channel common to all devices. Everything works fine for a while (a few days to a few weeks), but then the subscribe callback on the server stops firing whenever there's a message sent (the messages show up in the PubNub debug console). The server can publish normally though. The subscription works normally again after I restart the server. Here's the relevant code snippet:

 pubnub = new PubNub({
    publishKey: "pub-key",
    subscribeKey: "sub-key"
});
pubnub.addListener({
    message: function (m) {
        console.log(m.channel);
        console.log(m.message);
        console.log(m.timetoken);
        console.log(m.subscription);
        if (m.channel == "telemetry") {
            handleTelemetry(m);
        } else {
            handleRequest(m);
        }
    },
    error: function (error) {
        // Handle error here
        console.log(JSON.stringify(error));
    }
});

pubnub.subscribe({
    channels: chans //chans is a list of channels
});

Is there some sort of default timeout that stops a subscription service? If so how can I disable it?

Looking through the reference I found this value that I can pass to the initialization, but I'm not sure if it's relevant to me.

I'm running a test right now in my development server with presenceTimeout set to 0 and it's working, but it's a very hard problem to recreate as it can take a long time to pop up so if anyone has any insight into this problem it'll be appreciated. I'll update weekly on how the test is going.

Right now my temp fix in production is to have cron restart the server everyday which is not really ideal for me.

Update 2/15/17

Did a test with the Pubnub debugging logs enabled. It stopped working again. Below are the last few lines of the log.

The heartbeats seem to be coming in normally. I rechecked the logs before I restarted it and another one was recorded at 6:44 utc. The object after the heartbeat at 6:39 is a message that was successfully published to one of my devices. Any ideas?

You can see the full log here. The logs from my code aren't formatted very well so excuse the mess.

回答1:

Running NodeJS in Docker on Digital Ocean

With a PubNub Socket Connection to receive updates on data channels you can setup a node.js app which runs continuously. The following example is runnable from command line when you name your file app.js.

npm install pubnub

node app.js

// app.js

const PubNub = require('pubnub');
const pubnub = new PubNub({ publishKey : 'demo', subscribeKey : 'demo' });

pubnub.addListener({
    message  : m => console.log(m) 
,   error    : m => console.log(m) 
,   presence : m => console.log(m) 
});

pubnub.subscribe({ channels : ['a'] });