DZone
Cloud Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > How to Write JavaScript Actions for OpenWhisk on Bluemix

How to Write JavaScript Actions for OpenWhisk on Bluemix

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

Niklas Heidloff user avatar by
Niklas Heidloff
CORE ·
Mar. 19, 16 · Cloud Zone · Tutorial
Like (2)
Save
Tweet
7.01K Views

Join the DZone community and get the full member experience.

Join For Free

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.

JavaScript Bluemix

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Upsert in SQL: What Is an Upsert, and When Should You Use One?
  • Top 20 Git Commands With Examples
  • How to Leverage Method Chaining To Add Smart Message Routing in Java
  • 11 Reasons To Use Selenium for Automation Testing

Comments

Cloud Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo