Getting Started with Webtask.io and IoT
I was in a very interesting workshop this past weekend on
Webtask.io presented by Auth0’s
Glenn Block. Webtask.io brings to the table a thoughtfully designed
in-browser editor, a powerful CLI and impressive startup times that live up to
their messaging of “run(ning) code in 30 seconds”. While the tool chain
appears to favor reactive implementations akin to AWS Lambda or Azure
Functions, they do offer the capability to run jobs on a cron like schedule.
What better way to explore this feature set than to implement a trivial IoT device simulator … so let’s get started.
Get started by installing the Webtask CLI available
here. Open a Powershell terminal, create a
new folder in your source directory and cd into it:
mkdir webtaks; cd webtasks
npm init and set up a basic package/project scaffold.
- package name:
My First Webtask
- entry point:
main.jsAdd the Azure IoT Device SDK for Node:
npm install azure-iot-device-amqp --saveOpen VS Code in the project folder and create a new file called
In main.js, add a shell function for the webtask that includes a callback to indicate function completion as follows:
Remove the body of the above function and add the Azure IoT Device library, a connection string and create a device client. You can follow these instructions to learn how to create an IoT Hub, add a test device and get a device connection string.
We’ll now define an array to hold logging data, create a simple logging method and alias the function completion callback passing it our log data.
The last elements we need are a callback for the client connect function and a call for the client to open it’s connection to IoT Hub.