Use Node.JS for a Rest Remote Service

December 24, 2012

This guide is divided into two parts, the first one describes how you can setup a node.js server, that provides a REST interface, runs permanently and handles websocket connections to a client. The second part describes how you can post notifications from a Ruby on Rails app to your node.js rest remote service (from part one), for spreading messages to connected clients.
If you want to add the following functionality to a production system, you should have a look at HAProxy for your Node.JS Websocket Application .

    Technologies:

  • Node.JS
  • Socket.IO
  • Ruby on Rails
  • Typhoeus


Part 1

First of all you have to install node.js, to be able to write server side javascript code. From node.js you can download node.js executables/source-code, or if you are on Ubuntu take a look at Setup4Ubuntu .

Create a Folder, for example “var/www/nodejs/example” and change into your new directory

Make sure npm (Node Package Manager) is installed

Install Package Socket.IO by typing

Next step is to write some server side code, that connects to the client side via websockets.
Create a new file “server.js” and insert:

Now you should be able to start your node.js server by running the following command in your project folder “var/www/nodejs/example/” :

//After that you have to add some javascript code for your client side js, that connects to your server. Create a folder “js” and place the file “script.js” in this folder with the following lines:

Now you have to create a “index.html” which represents the user page. This File is just a quick way to test the websocket functionality. It should be easy to adopt this file for your Ruby on Rails Views.

If you want to run your node.js server as a kind of durable service take a look at HAProxy for your Node.JS Websocket Application and create an upstart script. This is only relevant if you want to run your service on a production system. If you want to test your application locally simply type as described before:
node server.js

Part 2

The Typhoeus gem is an alternative to the net::http library. The typhoeus gem runs requests via hydra for a better Performance. Instead of the described Typhoeus gem you can use net::http, or one of the many other gems that handles HTTP requests.

First you have to add typhoeus to your Gemfile
gem 'typhoeus'

After you performed a bundle install, you can send Messages to your node.js Server by adding the following code to a controller of your Rails App:

Notice: If you don't want to use Typhoeus you can use Net::HTTP instead, but you have to do the exception handling yourself.

Now you have to start your node.js server, open the index.html file from part1. Then you have to start your Rails application and open the view that uses the controller action with your code. After that you should see the message on your index.html file.

Notice: If you are using this example for production you should add a kind of authenticity token to the params and decrypt it in your node.js server. If your node.js application and your Ruby on Rails application are running on the same machine you should only accept messages from localhost.

A much faster way is to use the pusher gem. You can find a detailed description of the API on github.

Leave a Comment

You must be logged in to post a comment.