Communicate between a Rails app and a Node.js app

前端 未结 5 2130
长情又很酷
长情又很酷 2021-02-13 14:41

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 communic

相关标签:
5条回答
  • 2021-02-13 15:05

    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.

    0 讨论(0)
  • 2021-02-13 15:13

    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.

    0 讨论(0)
  • 2021-02-13 15:14

    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 these two shows what are the possible ways how to solve the authentication problem.

    0 讨论(0)
  • 2021-02-13 15:17

    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.

    0 讨论(0)
  • 2021-02-13 15:26

    What about keeping Rails and use Faye?

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

    0 讨论(0)
提交回复
热议问题