Communicate between a Rails app and a Node.js app Communicate between a Rails app and a Node.js app node.js node.js

Communicate between a Rails app and a Node.js app


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 8124server.listen(8124, "127.0.0.1");

And in RoR you can connect to the socket

require 'socket'      # Sockets are in standard libraryhostname = '127.0.0.1'port = 8124s = TCPSocket.open(hostname, port)while line = s.gets   # Read lines from the socket  puts line.chop      # And print with platform line terminatorends.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.


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.