Building a component in Node.js or Java to deploy it on the platform is described in the following documentation articles:

Those articles cover the architecture and the logic of the component and give you necessary tools to start your own project. The component development path is pretty standard and known to any developer who had worked with GIT at any stage: Code ⇒ Push ⇒ Connect ⇒ Run & Monitor.

What if you are still brainstorming your component building and want to quickly test one piece of code to see how it runs on our system?

If you are developing in Node.js, then you are in luck, since we have just the right tool for you to run and troubleshoot your code on the fly - pretty much real-time coding if you will.

Code Component - Run & Troubleshoot now

The Code component is one of the essential tools deployed on our production. Jump to the sections:

How to use it?

Pretty much the same way that you would use any other component in our system. It is deployed by default to production, so no need to deploy it yourself (although you could if you have extended it yourself). In our Dashboard start building your integration and include the Code component as well. You will see a picture similar to the one below:

However, don't let the simple look fool you - it has a full-fledged interface with many very useful features like the ones you would expect from your favourite desktop developing tool:

  • Syntax highlighting - a standard for any online real-time coding interface
  • Code auto-completion - again a standard that you got used to from your desktop tool
  • Support for number of variables and libraries within the context of the execution
  • Support for ECMAScript 2015 javascript standard
  • Run and troubleshoot within the designer interface.

Available Variables and Libraries

Here are the available variables and libraries that can be used within the context of execution. The most up-to-date list can always be found in be used within the context of execution. The most up-to-date list can always be found in code.js of the component. Below is a sample for the reference:

Code component usage Examples

Here are several examples where it's possible to see how can Code component be used. For purposes of displaying the functionality, we call our own Rest API.

In the examples below, we make an HTTP request to retrieve your user information from our API and pass this information to the next stage of integration. We use Environment variables to retrieve your API username and API Key which is already defined in your account while logged into the platform Dashboard.

The information that we are accessing is normally given back in the JSON form like:

HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": "54f4be3fe7d5224f91000001",
  "first_name": "John",
  "last_name": "Doe",
  "email": "test@example.com"
}

Using Callbacks

The simplest way to retrieve this information could be done using javascript callbacks.

console.log('Starting Node.js code execution');

var opts = {
    uri: 'https://api.elastic.io/v1/users',
    auth: {
        user: process.env.ELASTICIO_API_USERNAME,
        pass: process.env.ELASTICIO_API_KEY
    },
    json: true
};

request.get(opts, function(err, response, body){
    if (err) {
        console.log(err);
        return emitter.emit(err);
    }
    
    if (response.statusCode != 200) {
        return emitter.emit(new Error("API responded with", response.statusCode));
    }
    
    // create message to emit
    var data = messages.newMessageWithBody({
        fullName: body.first_name + " " + body.last_name,
        email: body.email,
        userID: body.id
    });

    // Emit the data event
    emitter.emit('data', data);

    console.log('Message emitted');
});

For a simple use case, this is sufficient, however, if we then decide to include more options to connect different servers it could simply block your entire integration flow if the proper routines and handlers are not implemented in your code. This usually involves handling and processing the callback errors and nesting more and more structures. The highly nested code is difficult to troubleshoot and it is not called callback Hell for no reason.

Using Promises

We could avoid the callback Hell by simply using promises which would transform the example above into something short and robust:

function* run() {
    console.log('Incoming message=%j', msg);
    return request.get({
        uri: 'https://api.elastic.io/v1/users',
        auth: {
            user: process.env.ELASTICIO_API_USERNAME,
            pass: process.env.ELASTICIO_API_KEY
        },
        json: true
    }).then(function transform(res) {
        return {
            fullName: res.body.first_name + " " + res.body.last_name,
            email: res.body.email,
            userID: res.body.id
        }
    });
}

Using Function Generators

Going one bit further we can use Function Generators to make our example even more robust. Notice we have added an additional call to our API inside.


function* run() {
    console.log('Incoming message=%j', msg);
    var auth = {
        user: process.env.ELASTICIO_API_USERNAME,
        pass: process.env.ELASTICIO_API_KEY
    }
    var result = yield request.get({
        uri: 'https://api.elastic.io/v1/users',
        auth: auth,
        json: true
    });
    var result2 = yield request.get({
        uri: 'https://api.elastic.io/v1/teams',
        auth: auth,
        json: true
    });
    return {
        fullName: result.body.first_name + " " + result.body.last_name,
        email: result.body.email,
        userID: result.body.id,
        teams: result2.body.map((team) => team.name).join(',')
   }
}

For this example yield would make sure that response would be given only when all these requests in the above example are executed.