Code
Introduction
The ClearBlade IoT Platform utilizes a microservices model powered by a Code Engine
. The code engine allows for JavaScript (ES5) and has several built-in integrations to interact with your IoT assets. Following microservice practices, a new execution context is sandboxed and created upon invocation. Once the process is complete, it is destroyed.
There are four code asset types:
Microservice: A short-lived service expected to be completed within a fixed time (or is killed otherwise); default execution timeout is 60 seconds.
Stream service: A service configured to handle requests continuously with an infinite execution timeout.
Configuration: A library that contains a key-value object to be used as constants.
Library: A custom JavaScript library that can be imported into one or more code services.
Here is a code service tutorial.
Purpose
Microservices, a software development technique, allows for a high degree of modularity, scale, and parallelization – all essential for effectively building and running full-scale IoT applications.
Libraries
Libraries are reusable code sets that one or more code services can import.
Native libraries
Native libraries are available in every system and have an underlying Golang implementation. Each library is optimized for computational performance.
These libraries can be imported using requires.
Library | Description | Docs |
---|---|---|
clearblade | Interacts with collections, messaging, users, edges, and devices | |
log | Appends messages to a log | |
http | Makes outbound HTTPS requests | |
geo | Geospatial computation library | |
crypto | Creates cryptographical hashes | |
file_writer | Write files to host the filesystem | |
jsutil | JS utility library | |
mailer | Send an email directly from the host machine | |
net_cb | Net library | |
oauth2_lib | OAuth utility library |
Custom libraries
A developer can create a custom library with custom logic. Custom libraries can also be created by importing NPM packages. Here is an NPM import tutorial.
Services
A service is a short-lived microservice expected to be completed in a finite time capped by the execution timeout field in its settings (it is killed after the timeout). The default is 60 seconds.
Lifecycle
Invocation
A service can be invoked by:
1. A REST request to the API endpoint
2. Trigger event
3. Timer event
Our SDKs can be used as a REST client to accomplish the first REST option.
Context
Each code service has a name corresponding to an autogenerated function name.
For example, a code service named analyze
will have an autogenerated function like:
function analyze(req, resp) {}
This function will have two parameters, similar to Node’s Express library.
req
req
is an object containing these contextual values:
Key | Description | Type | Example |
---|---|---|---|
userEmail | The user’s email who invoked the service | string | user@clearblade.com |
params | JSON with parameters. The structure is based on the invocation type | object | {“key”:“value”} |
systemKey | System key for the system in which the code service is running | string | e095a19d0b94e48dee9bff87e5fd01 |
systemSecret | System secret for the system in which the code service is running | string | E095A19D0B9ACD9EFA86AEEDFCC001 |
userToken | Invoking the user’s OAuth token | string | U1hNa7o4bEXpiE4aGhb-79 |