-
Notifications
You must be signed in to change notification settings - Fork 7
Loopback connector 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.
This project provides following nodes
Name | Description |
---|---|
async-observer |
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 |
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 |
End sync observer node returns control to loopback application by calling loopback observer's next() callback. |
model-observer |
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. |
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`
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*
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.
Following table illustrates each node in the above diagram -
Node | Configuration details |
---|---|
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
.
Following table illustrates individual node settings -
Node | Configuration details |
---|---|