Skip to content Skip to sidebar Skip to footer

Communicate Between A Rails App And A Node.js App

This question follows a previous one: Shall I use Node.js Instead of Rails for Real-time WebApps? The question: What's the best way of communicating between a Rails app and a Node.

Solution 1:

Why not open a TCP socket for communication between node & RoR ?

var net = require('net');

// create TCP servervar server = net.createServer(function (socket) {
  // write down socket
  socket.write("Echo server\r\n");
  socket.pipe(socket);
})

// start server listening on port 8124
server.listen(8124, "127.0.0.1");

And in RoR you can connect to the socket

require'socket'# Sockets are in standard library

hostname = '127.0.0.1'
port = 8124

s = TCPSocket.open(hostname, port)

while line = s.gets   # Read lines from the socket
  puts line.chop      # And print with platform line terminatorend
s.close               # Close the socket when done

Then just write an abstraction on top of this TCP socket to synchronize your communication nicely and without requiring low level fiddling.

Solution 2:

Why do the apps need to communicate?

If you simply need a Rails app to get some realtime data into the browser, then using a node.js server app and Socket.IO would be sufficient.

You have to remember that any Rails apps, is actually two applications, one written in Ruby running on the server, and one written in Javascript running on the client. They usually communicate over HTTP, sometimes with AJAX and sometimes not. Which part of your app needs the functionality of node.js?

If it is the case that the app deals with login, then displays a web page, and then continually refreshes that web page with real-time data, you only really get a benefit from node.js for the realtime data refreshes whether you do it with AJAX polling or with Websockets. Shared databases are a nice way for apps to communicate, but not for realtime.

To make it clear, if you are an expert in Ruby with Rails, you will be more productive if you add a node,js server app and only use it for high-volume data, such as realtime updates. You then have a hybrid web app that leverages the best of both platforms.

Solution 3:

What about keeping Rails and use Faye?

the latest Railscast is awesome: http://railscasts.com/episodes/260-messaging-with-faye

Solution 4:

One way is to have a common back-end database or some kind of memory storage which will act as intermediary layer between the two technologies. Popular is for example to use NoSQL DB like Redis which is fast, memory based and supports advanced data structures which are handy for this scenario. Also node.js and RoR both have a good client libraries for communication with Redis.

I would say the main problem is in initial authentication between the two separate systems which both needs to be synchronized. There are similar questions/answers related to this topic which may come useful to read, for example thesetwo shows what are the possible ways how to solve the authentication problem.

Solution 5:

It depends on exactly why you're separating the functionality from one to the other. Rails supports REST based separation without any extra work on your part. It's built based on resources from the ground up. That means it would be very simple for you to use an http.Client (or something like Restler) to query against it. You can certainly do the exact same the other way around, using standard Node.js routing (or something like Express) and an HTTP client for Ruby (such as Typhoeus). Though this method incurs the overhead of using a full HTTP request (not necessarily a problem if on an internal network). If you are looking for a more speedy way of communication, I'd say you could go about it using a persistant socket as Raynos suggests.

Depending on your need, I would suggest that using two separate systems creates extra code complexity, and it may be best for you to reduce it to one framework/language. I'm all for Service Oriented Design, but Rails is a pretty heavy weight and may slow down your over all response times, even with having Node.js working with it.

Post a Comment for "Communicate Between A Rails App And A Node.js App"