PubNub, the real time application messaging and cloud infrastructure service, recently announced their new BLOCKS feature. BLOCKS allows for scalable, serverless cloud computation (i.e. mutation) of payloads moving through its Data Stream Network. Those messages can then be sent to other PubNub event handlers, published to channels for use by subscribers, or in our case, pushed to third-party APIs such as Losant.
As we did previously with the AWS IoT Button, we will push data from a cloud computing service to the Losant platform using a webhook. We'll then trigger a workflow from that webhook and set the state of a device.
For an extra challenge, we will also use PubNub's BLOCKS to send a separate message to Losant using our REST API – first by authenticating the request as our device, and then setting that device's state.
First, let's do the necessary setup on the Losant side before we send data to our platform.
Device Type
is "Standalone", and that it has three attributes:
Number
Number
String
Once these steps are complete, it's time to switch over to PubNub.
PubNub has a fantastic getting started tutorial for working with BLOCKS. While I recommend going through it in its entirety, for our purposes we will only follow the "Creating an App" and "Creating the Block" sections before breaking off on our own.
Once you have completed PubNub's steps to create an app and a block, you'll need to create three separate Event Handlers to complete our tutorial.
After creating the BLOCK, you'll see a call to action to create your first Event Handler. Click the "Create" button, and in the modal that appears ...
Event Handler name
.Channel name
.Select an event type
.Click the "+" tab button to the right of your "Random Number" event handler tab (as shown in the picture above). This will open another modal for creating an event handler ...
Event Handler name
.Channel name
.Select an event type
.Start setting up the third and final Event Handler by once again clicking the "+" tab button, and in the modal ...
Event Handler name
.Channel name
.Select an event type
.Click the "Start block" button in the top right corner, and you will see the block and all three Event Handlers come to life in the console. Then, switch back to the "Random Number" tab and click the "Publish" button in the "Test Payload" section. (Leave the payload set to its default value.)
In the "Random Number" tab, you should see that both Promises succeeded; and under the "Losant Webhook" and "Losant REST" tabs, you should see successful responses from Losant.
Back in the Losant platform, navigate to your application's main page and, if you click "Publish" in PubNub again, you'll see the device's state being set both by the webhook and by the API call in the application log.
We've demonstrated how using PubNub's new BLOCKS feature makes it easy to send your real-time messages to Losant, both using a webhook and also using our REST API. We encourage you to check out their growing BLOCKS catalog and find new ways to connect your Things yours PubNub and Losant.
Working on a PubNub-to-Losant project of your own? Let us know in the comments!