Integrating Node.js with Ruby on Rails

Integrating Node.js with Ruby on Rails

Integrating Node.js with Ruby on Rails

A runtime environment for networking and server-side purposes, node.js has built a reputation as a multiple platform set up. On the other hand, Ruby on Rails is a robust web application framework, with Ruby as the language. Now that the introductions are done, we consider in this article, the viability of improving the performance of Ruby on Rails applications with the use of Node.js.

Node.js & Rails in Application

For the purpose of this article, take into account the sample product. Map integration is where all interaction done with the map is handled by AJAX requests once the entire page gets loaded (like setting up markers). Phusion Passenger is the web application server used. Nginx – the reverse proxy and HTTP server. So, for example, if the application invites 100 users at a time, only 30 users were able to log in at a time, rest of the users had to queue up to expect any response, thus pushing the application into chaos. Building up the amount of passenger workers would only temporarily help, fulfilling only a fixed amount of concurrent requests.

This is where application creators thought that node.js would be useful. One advantage was that requests won’t be able to queue up, the time spent on handling would go down drastically. The other merit – reducing time in the user request-application response cycle.

Many different node processes shall be handling several requests of AJAX. Certain conventions had to be set in place, to gel it with a Rails project. Automated deployment was managed with an automated toolkit like Capistrano. Code structuring was done with the aid of Express framework, the only extra NPM model used was node-postgres.

Application Deployment

Continuing with our example, there are little tools that can aid in the deployment process, ones that go into controlling Node.js. This particular tool, a command-line interpreter (CLI) called Forever, acts as a supervisor to the entire process, even while it restarts the application, when required. Thanks to the deployment techniques, managing several NodeJS applications becomes a comparatively easy task. A drastic 30% improvement was seen in the user request-application response time. No user requests were no longer in the sidelines post the Node.js additions. The problem arising from extra requests were no longer an issue. The advantage of using Node.js as compared to the usual development stack was quite evident.

Node.js Usage with Ruby on Rails

Node.js in combination with Ruby on Rails serves one particular purpose, as has been described above. On a web application, handling a number of user requests at a time can be daunting. As was displayed, Node.js helps in boosting the user request & response time. Other advantages of Node.js:

  1. With Node.js you may have to write more code. The advantage – what particular line of code will have what effect will always be clear.
  2. Using Node.js results in the application leads to making it a lot faster than it was earlier, apart from performance improvement.
  3. Making additions and altering the code is possible in Node.js, thus giving it flexibility and range as a runtime environment.
  4. Porting time occurs faster on Node.js.
  5. If you are on to a big project, then trust that it will be accomplished within a lesser time frame in Node.js.
  6. You require lesser number of servers to use node.js, even as possibilities of increase in traffic is many-fold.
  7. Node.js is also the preferred runtime for mobile applications, after all mobile phones is where the market is moving to.
  8. Scalability is something node.js can handle without any interruptions.

Another Rails & Node.js Instance

Here’s another example of Rails and Node.js used to great effect in the making of a chat application. Even as we use Rails for building a main application, using Node.js for its web-socket capabilities is an ideal premise. The data structure server, Redis acts as a bridge between Node.js and Rails, adding sturdiness to the structure.

Rails for a chat application is an apt choice, for the framework and support in building web applications. Among other functions, things like creating a separate chat room or registration process for the user works great with Rails. Ensuring a smooth run in the request-response part is Node.js territory. MySql will be used as storage space for user information and chat room data. AJAX post will be in use when users send messages.

The server receives the message. For asynchronous processing, Torquebox was used. The latter implements HornetQ, the messaging project. This is how posting of the message takes place. Chat history details are obtained through MongoDB. Redis sends a notification to the Node.js application that a message has been received.

Finally, an exclusive Rails app with Node.js response capabilities add speed to the proceedings. Scalability is enhanced by Redis, JRuby, MongoDB, Node and HornetQ. This is how a basic chat application can be up and running, thanks to the amalgamation of Rails and Node.js.