wissel.net

Usability - Productivity - Business - The web - Singapore & Twins

Running NodeRED on Heroku with Salesforce


I am a big fan of NodeRED, so I was delighted to see nodes available to connect to Salesforce.
Since the package was a little behind and support for platform events was still missing, I had a chat with Jeff and we agreed that I would maintain the package moving forward.

I updated the package dependencies to current version and made two main changes to the functionality:

  • allow credentials to be read from environment variables. This allows for easy deployment to Heroku
  • Support for Salesforce platform events

Deployment to Heroku is a breeze, but requires a few steps that I document here.
The instructions require a basic understanding how Heroku works, so you might want to check out a tutorial first. There are some challenges to overcome:

  • I want to be able to test local and on the server
  • Storage on Heroku is ephemeral. Any changes written to the server's file system get reset on restart
  • Deployment should happen via version control

Prerequisites

  • Current version of NodeJS installed (I usually stick to the LTS version)
  • GIT client installed (or GIT as part of your favorite IDE)
  • Heroku CLI (a.k.a Toolbelt) installed. Install using npm install -g heroku-cli

Project setup (Command line work)

  • Create a new directory NodeRedDemo and change into it
  • Initialize a new project npm init - fill in suitable defaults
  • Create a file .gitignore (content see below)
  • add NodeRED and the Salesfore nodes npm install -s node-red node-red-contrib-salesforce
  • add a line into package.json (see sample below) in scripts: "start": "node-red --settings ./.node-red/settings.js --userDir ./.node-red"
  • create the directories scripts and .node-red
  • copy the settings.js file from node_modules/node-red to .node-red - We will edit that file later
  • initialize git: git init
  • add initial files to git: git add --all and git commit -m "initial creation"
  • login to heroku heroku login
  • add Heroku: heroku create
  • deploy: git push heroku master

Your application should be running on Heroku now. To learn how to use NodeRED, check out a tutorial. Besides running on Heroku you can start the application on local too. Just use npm start and load localhost:1880.

What is missing: protect your NodeRED editor, connect to Salesforce and of course: create your flows.

Keep in mind: every time you redeploy or restart the app, anything stored, including your flow definitions, gets lost.
For permanent results I create my flows in the local instance of NodeRED and commit the flow file (in the .node-red directory) to git, so it becomes part of the deployment.

Utility scripts

In my script directory I have 2 scripts one local.sh and one heroku.sh. Remember: the scripts directory is excluded from git. The scripts are my shortcuts to setup the right environment variables, so heroku and local run with the same credentials. In my NodeRED flows I named the connection to Salesforce SFDC. This is used as prefix for the environment variables. You could set the variables in the Heroku UI too.

settings.js

To setup NodeRED security, check the documentation. Eventually I will reconfigure my instance to authenticate aginst Salesforce, but that's a different story for a different time.

 flowFile: (process.env.NODE_RED_FLOWFILE) ? process.env.NODE_RED_FLOWFILE : 'flows.json',
adminAuth: {
        type: 'credentials',
        users: [{
            username: process.env.NODE_RED_USERNAME,
            password: process.env.NODE_RED_PASSWORD,
            permissions: '*'
        }]
    },

local.sh

!/bin/bash
export NODE_RED_USERNAME=RedAdmin
export NODE_RED_PASSWORD=[somepassword]
export SFDC_UserName=[salesforceuser]
export SFDC_PassWord=[password]
export SFDC_ConsumerKey=[somekey]
export SFDC_ConsumerSecret=[somesecret]
# Start in debug mode, remove --inspect if you don't need it
node --inspect node_modules/node-red/red.js --settings ./.node-red/settings.js --userDir ./.node-red

heroku.sh

You can do all this in the web GUI too

!/bin/bash
heroku config:set NODE_RED_USERNAME=RedAdmin
heroku config:set NODE_RED_PASSWORD=[somepassword]
heroku config:set SFDC_UserName=[salesforceuser]
heroku config:set SFDC_PassWord=[password]
heroku config:set SFDC_ConsumerKey=[somekey]
heroku config:set SFDC_ConsumerSecret=[somesecret]

.gitignore

node_modules/
scripts/
.npm
#NodeRed runtime stuff
.node-red/.config.json
.node-red/.sessions.json
.node-red/*.backup

package.json

{
    "name": "nodereddemo",
    "version": "0.2.1",
    "description": "NodeRed Demo as Salesforce Middleware",
    "engines": {
        "node": ">8.0"
    },
    "scripts": {
        "start": "node-red --settings ./.node-red/settings.js --userDir ./.node-red",
        "debug": "node-red --inspector --settings ./.node-red/settings.js --userDir ./.node-red",
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [
        "nodered",
        "salesforce",
        "middleware"
    ],
    "author": "swissel@salesforce.com",
    "license": "ISC",
    "dependencies": {
        "node-red": "^0.18.2",
        "node-red-contrib-salesforce": "0.2.1",
        "node-red-dashboard": "^2.6.2"
    }
}

More on NodeRED, and Salesforce to follow in future posts.

As usual YMMV


Posted by on 14 February 2018 | Comments (2) | categories: Heroku NodeRED Salesforce

Comments

  1. posted by Chetan on Sunday 29 September 2019 AD:

    Excellent.It would be great if you could follow up with use cases.I have installed the app on heroku , now how would i move forward, what brilliant things can i do with it specific to Salesforce.


  2. posted by Chetan Anand Singh on Sunday 29 September 2019 AD:

    Excellent.What else can i do with it. I have installed the app on heroku, what uses cases can i achieve with it?