Skip to content

Loopback connector for Node RED

sharansev edited this page Mar 17, 2016 · 6 revisions

Loopback connector nodes for Node-RED

Loopback is a powerful NodeJS framework to create REST APIs based on declarative model definitions. Node-RED is yet another powerful tool to wire together APIs and online services.

This project provides a set of custom Node-RED nodes which can be used to integrate Node-RED with any loopback application.

This integration greatly enhances the existing capabilities of loopback application by allowing to observe the application in Node-RED and inject code at run time without touching the original application.

Available Node-RED nodes

This project provides following nodes


Name Description
async-observer image
Input node which registers an observer on given loopback model and returns to loopback application immediately. This node is suitable to start Node-RED flow asynchronously when any loopback model event occurs.

This node registers an observer by calling Model.observe ('access/before save/after save', function (ctx,next){...})
The entire loopback ctx is copied in Node-RED message context msg.ctx for rest of the Node-RED flow to use. The next() callback is called immediately to return control to loopback application.
sync-observer image
Input node which registers an observer on given loopback model and starts Node-RED flow. Control is not returned to loopback until sych-observer-end node is called. This node can be used to attach Node-RED flow which can alter the loopback ctx parameters.

This node registers an observer by calling Model.observe ('access/before save/after save', function (ctx,next){...})
The entire loopback ctx is copied in Node-RED message context msg.ctx for rest of the Node-RED flow to use.

IMPORTANT NOTE - Control to the loopback application will not return until sync-observer-end node is called. Any flow that starts with sync-observer must end with sync-observer-end otherwise the loopback application will be stuck forever.
sync-observer-end image
End sync observer node returns control to loopback application by calling loopback observer's next() callback.
model-observer image
Input node which register's an event listener on any generic javascript event emitted by loopback model.
This node calls Model.on ('event name', function (payload){}) and passes on the payload in Node-RED message context (msg.payload) for rest of the flow to use.

Getting started

Installation

npm install git+https://github.com/EdgeVerve/loopback-connector-nodes-for-Node-RED.git 

or

npm install [email protected]:EdgeVerve/loopback-connector-nodes-for-Node-RED.git`

Starting Node-RED from within loopback application

Add a new boot script in your loopback application with following code in it -

var nodeRed = require("loopback-connector-nodes-for-Node-RED");

module.exports = function(server, callback) {
    nodeRed.start({port:22081}, function() {
	callback();
    })
}

You can also specify following options while starting Node-RED

var options = {
    port: <port number on which Node-RED server to be started>,
    settings: {
        httpAdminRoot: ...,
        httpNodeRoot: ...,
        userDir: ...,
        nodesDir: ...,
        flowFile: ...,
    }
}
// Please see default settings.js file in node_modules/node-red for other options for settings.

Upon the application startup, Node-RED server will be available to access on *http://localhost:22081/red*

image

Usage examples

Inject filter condition in GET calls

Consider there is a loopback model called CountryCode and an API /countryCodes is exposed. This API returns list of country codes setup in the application. Now suppose there is a need to add restrictions on the list of country codes returned by this service at run time. This use case can be easily achieved by adding following Node-RED flow. This does not require any code change in the actual API.

image

Following table illustrates each node in the above diagram -


Node Configuration details
image image
image image
image image

Observe and log API access

Consider there is a need to observe APIs being accessed and log the information into some log file or send it over a channel like MQTT for further processing.

Such an use case can be achieved by using asynchronous loopback observer. The following flow attaches an observer hook on access of PersistedModel. So this Node-RED flow will get called on accessing models which has base as PersistedModel.

image

Following table illustrates individual node settings -


Node Configuration details
image image
image image