Over a million developers have joined DZone.

How to Write JavaScript Actions for OpenWhisk on Bluemix

Easily whisk up some server-side, event response code for apps and IOT devices.

· Cloud Zone

Download the Essential Cloud Buyer’s Guide to learn important factors to consider before selecting a provider as well as buying criteria to help you make the best decision for your infrastructure needs, brought to you in partnership with Internap.

In addition to Cloud Foundry, Docker, and Virtual Machines, Bluemix provides a fourth computing option, OpenWhisk (experimental at this point). OpenWhisk is an event-driven compute platform that executes code in response to events or direct invocations.

With OpenWhisk, you can develop server-side actions in JavaScript and Swift without having to maintain servers. The advantage is that you only have to pay for actual usage, not for peaked projections. This opens up several interesting use cases. For example, you can perform processing of data like videos and processing of data from IoT devices triggered by events. You can also build backend services for mobile apps to move logic to the backend and reduce network traffic. OpenWhisk also supports decomposing applications into microservices.

Below is a simple example of how to build a JavaScript action that leverages the Watson Language Translation service to translate text. This action can be invoked directly and also via events. For example, in a help desk application, you might want to translate tickets which have not been written in English to English so that support agents are able to understand them. Rather than doing this when tickets are stored or opened the OpenWhisk action could be triggered when new documents are stored in a Cloudant NoSQL database.

Here is the code of the JavaScript action (‘translate.js’). Parameters are passed into the main function. In this case, the action is asynchronous and returns data in the whisk.done function. The JavaScript code is executed via Node.js and you can use a number of available npm modules, in this case, the Watson module, watson-developer-cloud, is used to access the Watson service.


function main(params) {
    var tobetranslated = "";
    if (params.hasOwnProperty("tobetranslated")) {
        tobetranslated = params.tobetranslated;
    }

    var watson = require('watson-developer-cloud');      
    var language_translation = watson.language_translation({
        username: "xxx",
        password: "xxx",
        version: 'v2'
    });

    language_translation.translate({
        text: tobetranslated, source : 'en', target: 'es' },
        function (err, translation) {
            if (!err) {
                return whisk.done({tobetranslated:tobetranslated,
                                   result:translation});
            }
            else {
                return whisk.done({result: 'Error occured' });
            }
        });

    return whisk.async();
}

In order to deploy the action, a CLI is used, and the plan is to also support tooling via web browsers. Invoke the following commands to create and update actions.


wsk action create translate translate.js
wsk action update translate translate.js

For developing purposes, probably the easiest way to test actions is the CLI at this point.


wsk action invoke --blocking --result translate --param tobetranslated 'Hello'

The CLI internally invokes REST APIs which means that you can also interact with OpenWhisk via Curl or other programming languages. Authentication is done via name and password credentials provided by OpenWhisk.


curl -u "yourUserName":"yourPassword" -H "Content-Type: application/json" -X POST https://openwhisk.ng.bluemix.net/api/v1/namespaces/niklas_heidloff@de.ibm.com_BluemixInfo/actions/translate?blocking=true -d '{"tobetranslated":"Hello"}'

Here is the output of the Curl command.

whisk-sample1

Via the OpenWhisk dashboard on Bluemix you can see activities and logs.

whisk-sample2

To find out more check out the documentation, the open source project OpenWhisk and the image tagging sample from Frederic Lavigne.

The Cloud Zone is brought to you in partnership with Internap. Read Bare-Metal Cloud 101 to learn about bare-metal cloud and how it has emerged as a way to complement virtualized services.

Topics:
bluemix ,javascript ,swift ,server side scripting ,openwhisk ,open source ,ibm

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}