Will JavaScript type annotations kill TypeScript?
The creators of Svelte and Turbo 8 both dropped TS recently saying that "it's not worth it".
Yes: If JavaScript gets type annotations then there's no reason for TypeScript to exist.
No: TypeScript remains the best language for structuring large enterprise applications.
TBD: The existing user base and its corpensource owner means that TypeScript isn’t likely to reach EOL without a putting up a fight.
I hope they both die. I mean, if you really need strong types in the browser then you could leverage WASM and use a real programming language.
I don’t know and I don’t care.
Cloud Services / Edge Computing

Tutorial: Build a Serverless Application to Control a Smart Bulb

In the first part of this tutorial, we introduced Google Cloud Functions as a Serverless platform. In this installment, we will build an application based on Functions as a Service (FaaS) to control a Smart Bulb.
Feb 22nd, 2019 3:00am by
Featued image for: Tutorial: Build a Serverless Application to Control a Smart Bulb

In the first part of this tutorial, I introduced Google Cloud Functions as a Serverless platform. In this installment, we will build an application based on Functions as a Service (FaaS) to control a Smart Bulb.

I am a big fan of LiFX Smart Bulbs and their programmability. LiFX bulbs are WiFi enabled that can be controlled via simple REST API. If you already own a bulb, all you need is an API token that needs to be embedded in the HTTP calls made to the endpoint.

We will utilize the API of LiFX bulbs, Google Cloud Scheduler, Google Cloud Pub/Sub and Google Cloud Functions to automatically switch on the bulbs at 6 p.m. and switch off at 7 a.m. every day. We can also use the same configuration to interactively change the bulb settings such as the color, brightness, and its state.

The architecture of the application is pretty simple. The core of the app is a Google Cloud Pub/Subtopic to which we connect the Cloud Scheduler, Google Cloud Function, and an interactive CLI application. When a message is sent to the Pub/Subtopic, it triggers the function which calls the LiFX API. Cloud Scheduler and interactive apps simply publish the desired state of the bulb to the Pub/Subtopic. That payload is automatically forwarded to the Google Cloud Function.

Before we go ahead, make sure you have an active Google Cloud Platform subscription along with Google Cloud SDK installed in your development machine.

Configuring Google Cloud Pub/Sub

Google Cloud Pub/Sub is a message bus that can connect a variety of publishers and consumers. In this scenario, we rely on the Pub/Subtopic as the hub to fan out messages to one or more consumers.

Login to Google Cloud Console and access the Cloud Pub/Sub dashboard. Click on Create Topic button and give a name to the new topic.

That’s all we need do to within Cloud Pub/Sub. The topic we created is the most critical component of the application.

Creating the Google Cloud Function

We will now add the code snippet responsible to invoke LiFX API as a Cloud Function. This is invoked indirectly through Pub/Sub.

Access the Cloud Function dashboard and click on Create Function. Give the function an appropriate name and wire it to the Pub/Sub topic created in the last step by selecting Cloud Pub/Sub as the Trigger.

Replace the code of index.js with the below. Make sure that you replace the LiFX API token with your own API. Change the entry point to bulb under Function to execute.

The code is self-explanatory. The payload sent to the function via Pub/Sub is decoded into a JSON string, and sent to the LiFX API via the PUT request. The payload contains settings such as the one shown below

Testing the Functionality

We are all set to test our Serverless application by sending a JSON payload from the CLI. Assuming you have Google Cloud SDK installed, fire up the gcloud utility to publish to the Pub/Subtopic.

This step results in the bulb getting powered on with the color set to blue. Pretty simple and straightforward!

Now, let’s automate this process by configuring a cloud-based CRON job to turn on and turn off the light at a predefined time of the day. We will do that through Google Cloud Scheduler.

Configuring Google Cloud Scheduler to Periodically Publish to Pub/Subtopic

Visit Cloud Scheduler dashboard and create a job with the following configuration:

This step simply send the payload,

to the Pub/Sub topic every day at 6 p.m. Let’s also configure another job to switch off the bulb at 7 a.m.

That is it! We are done with the coding and configuration of our Serverless application. The beauty of this approach is simplicity. With less than 50 lines of code and a few clicks, we are able to deploy a fully-fledged application.

You can easily extend this application to synchronize the bulb with the sunrise and sunset timings. Create a Cloud Scheduler Job that invokes a Cloud Function which queries the sunset API and configures the smart bulb CRON jobs with the exact time to switch on and switch off.

We could have used HTTP trigger instead of Pub/Sub. But, there are many advantages of using a Pub/Subtopic. One of them is the fan-out approach where we can invoke multiple functions with the same payload.

Janakiram MSV’s Webinar series, “Machine Intelligence and Modern Infrastructure (MI2)” offers informative and insightful sessions covering cutting-edge technologies. Sign up for the upcoming MI2 webinar for a deep dive on accelerating machine learning inference with Intel Movidius.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.