our swiss φθηνα ρολογια are all in now.
This article is not intended to be an introduction or full guide on Node-RED but it will describe how we can run it in the cloud ! Thanks to its NodeJS base and the power of all services provided by Microsoft Azure, you can create an Azure Web Site for a NodeJS application that can host Node-RED so that you can access, control and develop on it from everywhere !
In this way, we don’t need to setup a server “on premise” or one of the most used boards like Raspberry Pi and Intel Galileo at home to host Node-RED. These solutions are good for makers or scenarios with a low level load on the executed “flow”. If we need a more scalable system with million of connections, the only available solution is to use a PaaS as Microsoft Azure.
Are you interested in to know how we can do it ? Let’s start !
Deploy to a Local Git Repository on Azure
The first step to do is to get the Node-RED source code from its GitHub repository because we need to change a little bit of code. For this step we have a lot of possible solutions thanks to the integrate source control feature provided by Azure for its Web Sites. When we create a Web Site, we can setup a deployment from source control so that modifying our repository (local or remote) we can update instantly our deployment in production in Azure. The source code can be hosted on Visual Studio Online, Local Git Repository (on Azure), GitHub, Dropbox, Butbucket, CodePlex or on a other external repository based on Git or Mercurial.
In the case of Node-RED, the two possible solutions are using a Local Git Repository or the remote GitHub repository. I will cover both for completeness so that you will be able to choose the best way for you needs.
First we have to create an Azure Web Site on the online management portal.
To setup a deployment from source control we can use the related link in the main page for the just created Web Site.
To use a Local Git Repository, we have to choose “Local Git repository” feature.
To enable Git publishing, the first time you use it, the management portal asks you to choose credentials (username and password). These credentials are common to all Web Sites so if you have already enabled them for other Web Sites, you will not be prompted to insert them. After this, Azure creates a local Git repository where we can publish our application and it gives us some information about how we can do it : download and install Git on the PC, create a Git repository locally and then commit the application to the remote repository on Azure.
In this case the local application is Node-RED and we need to download its source code zip file from official web site. Extract the files in a folder (in my case C:\Users\Paolo\Source\Repos\node-red-0.8.0) and then use the Git Bash (from the Node-RED folder itself) to create a Git local repository, add all application files and execute the initial local commit with the following commands :
git add .
git commit –m “Initial Node-RED commit”
(Remember that Git needs an email and a username to execute commit and to set them you have to execute git config command for user.email and user.name parameters).
The next step is to add a Git remote and push the master branch to the repository on Azure; from the management portal, in the Web Site dashboard, we can get the repository name. The commands to execute are the following :
git remote add azure [repository URL on Azure]
git push azure master
You will be prompted for password of your credentials related to Git repository and Azure Web Sites. After the operation is done, Azure starts the deployment operations and we can see its result in the Deployments tab on the management portal.
During this deployment phase, Azure executes npm (NodeJS Package Manager) for you to install all modules needed by Node-RED to work; it gets the information from package.json file.
It seems that we have just finished but if we try to navigate to the Web Site URL with our browser we aren’t able to see the visual tool to create the “flows”. To solve this problem we need to change a little bit of code inside settings.js file in the Node-RED application. The change is related to the uiPort parameter in the following way :
// the tcp port that the Node-RED web server is listening on
uiPort: process.env.PORT || 1880,
At the end of this article I will explain why the above change is needed.
After this change we need to commit it locally and to Azure with following commands so that the system can re-deploy the application.
git commit –m “uiPort setting changed”
Before using Node-RED we need just a little change. The visual tool that runs in the browser to create the “flows” uses WebSocket protocol that we have to enable in the Configure tab on the management portal for the Web Site. This step is needed only if we want to use the visual tool online to create “flows” who are JSON files that we can create locally and then deploy in Azure.
After saving it, we can now navigate to your Web Site to see the visual tool and the Node-RED instance up and running. You are now ready to create “flows” !
Deploy using a GitHub repository
The second possible way is to use a GitHub repository to deploy Node-RED. Of course, we can use the official repository but in this case we can’t modify the settings.js file as done before and the application can’t run on Azure. The only way is to fork the Node-RED official project to have your own repository on GitHub.
After logging into GitHub with your credentials, go to Node-RED repository and click on fork.
When the fork is completed you have a GitHub repository like this https://github.com/<username>/node-red so that we can clone it locally using Git Bash in the following way :
git clone https://github.com/<username>/node-red.git
After cloning, modify the settings.js file (as done before on uiPort parameter) and then commit the changes to the GitHub fork.
We don’t need to push this Git repository to an Azure repository because now we are going to create a new Web Site and setup source control that points to the GitHub fork.
After creating Web Site as previous, we have to setup the source control for deployment using GitHub.
Microsoft Azure connect to GitHub and asks you the authorization to use your account to access all your repositories. After this, you are prompted to choose the repository to connect and then link to it and deployment starts from there.
As before we need to enable WebSocket protocol to have your Node-RED application up and running !
Test Node-RED on Azure with a simple debugging flow
To be sure that Node-RED application is working properly, we can create a simple flow using only two simple nodes :
Add the two nodes above to the flow and the connect them in the following way :
Click on Deploy button (in the up right corner) to deploy the flow in the Node-RED instance and try to click on the square left to the inject node; as you can see, the timestamp is shown on the left Debug panel. Your flow is working on Azure !
IISNode …. the magic to have Node-RED on Azure
The main component of NodeJS application hosted on Azure is the IIS module named IISNode. Its advantages are the following :
The only need to execute a NodeJS application with IISNode is to apply minimal changes to the application itself like the listen HTTP port using the process.env.PORT environment variable. It explains why we can’t have Node-RED visual tool listening on a port like default 1880 but we need to change uiPort parameter to use default IISNode HTTP port for NodeJS application.
Node-RED is a great tool for wiring Internet of Things with a UI running inside a browser but sometimes we have the problem to host it. On premise or local solutions are good for simple scenarios but not for enterprise. In this case we can use the facilities of Azure to deploy NodeJS application (like Node-RED) adding to it the scalability and load balancing thanks to IISNode module inside IIS.
Hi Paolo, nice article, exactly what I was looking for. It almost works but I getting the error: Lost connection to server. Any idea what can be wrong? I tried several times, checking all the steps and everything seems fine. Thanks Lubo
View this page in another language: