问题描述
我目前正在使用Rails服务静态网页,并且我正在尝试使用NodeJ来处理我的应用程序的某些实时方面.
I am currently using rails to serve static web pages and I am experimenting with NodeJs to handle some real time aspect of my application.
通过让Nodejs写入数据库并从Rails服务器读取数据,我已经能够在Nodejs与我的Rails服务器之间进行单向通信.
I have been able to have do a one way communication between Nodejs to my Rails server by having Nodejs write to a db and my rails server reading from it.
现在我想用另一种方式,也就是Rails中的一个动作将触发Node.js中的一个动作.显然,我可能很笨,并且有一个节点不断轮询数据库服务器.
Now I want to do the other way, aka an action in Rails will trigger an action in Nodejs. Obviously I can be dumb and have a node continually polling the database server.
我有什么选择?
- 在两者之间设置RPC调用
- 双向设置TCP套接字
有更简单/更快捷的选择吗?
Are there easier/quicker options?
推荐答案
从技术上讲,您有很多方法可以使用进程间通信,如果您想轻松一点,我相信您应该看看 dnode 在TCP或命名管道上提供RPC,并且具有 ruby实现.使您进行RPC调用非常容易,并且由于它是TCP,因此您可以在计算机之间使用它.
Well technically you got a lot of ways for inter process communication, if you want something easy I believe you should have a look at dnode which provides RPC on TCP or named pipes and it has a ruby implementation. Making it very easy for you to do RPC calls and since it is TCP you can use it cross machines.
您也可以有一个消息队列,例如 zeromq ,但我认为这样做会产生不必要的开销.如果您有两个以上的进程互相交谈,那将是一件好事.
You can also have a message queue such zeromq, but I believe that will have unnecessary overhead. It would be good for cases that you have much more than two process talking to each other.
除了所有这些,如果您想要最小的延迟,并且您的进程都在一台机器上运行,我相信您应该使用命名管道和stdio进行通信,但是我不知道节点中的任何模块都会帮助您抽象化这一点,并且您必须在stdio上构建自己的RPC模块.
Beside all these if you want the minimum latency, if you're processes are both running on one machine, I believe you should use a named pipe and stdio for communication, but I don't know any module in node that will help you to abstract that, and you have to build your own RPC module on stdio.
这篇关于如何构建Nodejs服务器与Rails之间的通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!