New library to delay Publish to cloud: PublishQueue

Hi,
Recently published a new library called PublishQueue that delays (puts in queues, thus the name) the calling of cloud events (via Particle.publish) so it does not pass the limits defined by Particle As defined by Particle in the Particle.publish() documentation:

“NOTE: Currently, a device can publish at rate of about 1 event/sec, with bursts of up to 4 allowed in 1 second. Back to back burst of 4 messages will take 4 seconds to recover.”

it will record the time of last publish event and delay the next publish event until enough time as passed to guarantee the limits above are not hit.

Thoughts welcome.

3 Likes

Good one. I had ran into this problem as well and thought of the same concept, i.e. a queue that collects the publish requests and then releases them according to the requirements (timing constraints) of the cloud. I think I will give your’s a try.

1 Like

Cool idea, I tried to test the example and got a compile error:
publishqueue.cpp:2:26: fatal error: PublishQueue.h: No such file or directory
#include “application.h”

I just clicked on the .ino tab and clicked the Use Example button and hit Verify.

You need to change the first include statement to this

#include "PublishQueue/PublishQueue.h"

and also import the ElapsedMillis library.

But with Software Timers available now, it might be nicer to have the publishes done via one of these then by needing to call pubQueue.Process() over and over.

Should this timer be running at all times, essentially calling pubQueue.Process() or would it be better to start it via pubQueue.Publish (if not already) and then stop it after the queue has been emptied at the end of pubQueue.Process(). From the documentation it sounds like the only downside of keeping it running is a potential delay to other timers but it’s unclear if that’s less than any potential overhead in starting and stopping timers.

I also plan to check the success of the publish and requeue/retry if it’s not successful. I figure extending this queue system is better than blocking everything else while it keeps retrying during an outage.

Hi @tiagonmas

I’ve made an update on your library so it uses a software timer and removed the need for .Process(). I’ve also put it into a single .h file as the footprint is so small.

class PublishQueue {
public:
    PublishQueue() : publishTimer(1000, &PublishQueue::PublishTimerCallback, *this, false) {
        publishTimer.start();
    };
    void Publish(String eventName, String data) {
        node my_node = {.eventName=eventName, .data=data};
        my_queue.push(my_node);
    };
private:
    std::queue<node> my_queue;
    Timer publishTimer;
    void PublishTimerCallback() {
        if (!my_queue.empty()) {
            node my_node = my_queue.front();
            my_queue.pop();
            Particle.publish(my_node.eventName, my_node.data, 60, PRIVATE);
        }
    };
};

Kev