Internet of Customers – Node.js, Socket.IO and nForce on Heroku

Internet of Customers – Node.js, Socket.IO and nForce on Heroku

To complete the demonstration shown in Heroku, Arduino and Salesforce.com for Customer Service, we finally turn our attentions to the server side portion of the architecture. In this post we will share how Heroku was used to run a Node.js application that queried Salesforce.com via the nForce package to find an account matching the RFID code sent by the Arduino, finally sending any cases for the account over Socket.IO back to an application dashboard pictured below.

IOC-App
Thanks to the Start Bootstrap group for base UI template I modified.

Setting Up Your Application – OAuth
In order to communicate with Salesforce, you will need to setup an “app”. The steps are detailed at Creating a Custom Apps. Once you have gone through the steps, make note of your Consumer Key and Consumer Secret (blurred out in the example below). For the example, don’t worry about the callback URL (http://localhost:3001/oauth/_callback), as unlike standard OAuth patterns we are not going to directly interact with the Salesforce login – our Node.js application with nForce will do it for us. For full details take a look at the readme / setup information on the GitHub page for nForce.

Connected_App__IOCNode___salesforce_com_-_Developer_Edition

Using Heroku
If you have not used Heroku before, check out Using Web Sockets on Heroku to get a sense of what was involved getting a basic setup working with Heroku’s Toolbelt to get their sample running locally. This was used as the starting point for this proof.

It is especially important to become familiar with Node Package Manager, as it will act as the workhorse to ensure that you have all of the packages (think of them as “libraries” in more general development terms) required for the project. Thankfully, the package.json file within the project files will help to make getting the packages needed a single operation using the following command line.

It even ensures that it respects particular versions of packages. Take a look at the sample files for detail.

Key Node Files

  • app.js – this exact file name is not required, but in our example, app.js works as the heart of our application
  • package.json – contains references to the packages that are required for the application. Once the project is loaded into a directory, the “npm install” command can be run to download them.
  • .env – contains environment variables, making it easy to have various constants that are specific to an environment. This is where we place things like our OAuth codes in our proof.
  • views/dashboard.html – our sample dashboard that the Node application will communicate with using Web Sockets
  • procfile – what will be run by default when our application is loaded into a Heroku Dyno

Key Packages used in our Proof
Using the well documented nForce package from Kevin O’Hara, it was amazingly easy to communicate with Salesforce. The library acts as a REST API wrapper for force.com and allows easy interaction via SOQL with Salesforce objects.

We additionally use the express web application framework and the Socket.IO library to round out our proof, adding a dashboard UI and communication mechanism between clients and our Node application.

Working locally with the Node.js application was done via the Heroku Toolbelt, making running the sample as easy as typing the following foreman command once the application was setup.

If all goes well, you will see the following output within your console. The best way to get started is to ensure that you can run the basic web sockets sample from the Heroku developer center.

IOC-Sockets

Socket.IO on the Server and Client
Our proof uses Socket.IO to send the matching results of Salesforce Cases to the client on the basis of a custom field for the account that holds the RFID tag value. The

Server

The following excerpt from app.js shows the key areas where we include socket.io, then use its emit method to send our servicedata to the client over web sockets.

Client

After we include the socket.io library, we have access to the servicedata event, from which we can get our results in the message object. The following is an excerpt of the key sections.

Node.js Application with Socket.IO and nForce
The following is our main code. Please keep in mind that this is a proof and was put together to showcase a general idea. It is not intended for production.

Download Proof of Concept Code from GitHub