Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.js app in order to take advantage of both technologies?

Thanks

like image 409
donald Avatar asked Apr 04 '11 14:04

donald


People also ask

How do express and node work together?

Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.

Is NodeJS faster than Ruby on Rails?

Node. js takes more time, as you need to find the modules and follow the instruction for integrating them. Ruby on Rails seems to be faster, as you can perform some task, like database migration, with just a few commands. The learning curve is partially less than with Rails.

Is NodeJS better than Ruby on Rails?

Yes, Node. js is better than Ruby on Rails when it comes to the scalability of apps. Node. js processes a mix of Node clusters and workers and allows developers to expand the app's features compared to using Ruby on Rails as the server processors limit the workload.

Can you use JavaScript with Ruby on Rails?

While RoR is a server side web development framework, JavaScript is a client side programming language. Therefore, you can easily use both of them within a single tech stack.


3 Answers

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

var net = require('net');

// create TCP server
var 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 terminator
end
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.

like image 56
Raynos Avatar answered Oct 02 '22 12:10

Raynos


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.

like image 20
Michael Dillon Avatar answered Oct 02 '22 12:10

Michael Dillon


What about keeping Rails and use Faye?

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

like image 35
apneadiving Avatar answered Oct 02 '22 12:10

apneadiving