IN THIS ARTICLE
Subscribe to Our Newsletter
“That’s pretty cool! Wow!”
Normally, I like to quote customers and users, when talking about the value that our products bring to the market. In this case, this quote is attributed to my wife, when she asked me about the new Slack notifications that she was receiving, and my reply was: “Oh, we just released a new feature at PubNub and I hooked it up to our app, so it looks for inconsistencies in the blood sugar data and lets us know if there is a device communication problem.” Five minutes of coding and I had solved a problem nagging us for quite some time.
Every five minutes, my daughter’s continuous glucose monitor, connected somewhere on her body, streams off a number, her current blood sugar level. Too high, we have to give her insulin. Too low, some food. And if her various devices are out of range, so that they can’t receive the number and push it into the cloud and into PubNub, we don’t get the numbers and sometimes that results unsettling surprises. For example, if there was a device communication problem and we had been without data for thirty minutes or so, it could be that her blood sugar levels had changed dramatically during that time and as opposed to handling the situation with more grace and advance notice, we find ourselves scrambling to avert disaster.
Making Tricky and Schedule-Based Problems Trivial
Is this the longest preamble to a product blog announcement of a new feature? Yes! But hopefully it sets the stage for the value one can obtain from the new Functions event type: On Interval. Now, developers can have business logic execute, within the PubNub platform, on a periodic basis, independent of the realtime events occurring within their PubNub-powered application. Let’s say a developer wanted to respond to the business need for a periodic but realtime summary of activity across all of the customer support chat rooms. Prior to On Interval being released, the developer would have to not only write the logic, using the PubNub APIs to get all of the desired information, but also they would have to figure out where the logic would live, where it would run. A cronjob managed script running...where? Or they could put it into an On Rest function and then have a scheduled job - where am they going to put that? - run somewhere to call the function’s URI. Shoot. They have to stick their credentials in yet another place that has to be managed. None of these problems are insurmountable but together they represent a blocker to innovation and value creation.
Using On Interval to Solve Problems
What are some of the innovations that developers might want to do with Functions and On Interval? Here are just a few:
Pull data from a external REST API endpoint and push changes out through the PubNub network, thereby transforming non-realtime APIs into realtime APIs!;
Batch messages, retrieved from PubNub history API, and post to an external endpoint for out-of-band usage;
Generate per channel summaries, e.g. number of users, messages published, message actions sent, etc., and send out to business owners via email, Slack or Pubnub;
Whatever else you can do in 10 seconds or less!*
In my case, for my PubNub-powered application that helps my wife and I manage our daughter’s Type 1 Diabetes, I wanted to run my data validation logic, pulling in PubNub message history and evaluating timestamps and numbers to detect any problems and to do that every minute (60000 milliseconds!). I could have done this out of band, aka spin up a Heroku server, for example, run a script on a cronjob, but my backlog - professional and personal - is quite full and that was too much of a hurdle for me. With Functions and On Interval, life becomes easier and my problems get solved more quickly.
How to Set Up and Use Functions On Interval
It is easy to use. Just go create a function and select the
On Interval event type. It is at the bottom of the list (see pic below).
From there you’re off to the races. Right now, the minimum interval is 10 seconds (10000 milliseconds!) and the maximum is 60 minutes. Otherwise, you’re really dealing with a clean, simple coding setup:
That’s it. Pretty simple but really useful. Happy coding!