如何构建Nodejs服务器和rails之间的通信?
我目前正在使用 Rails 来提供静态网页,并且正在尝试使用 NodeJs 来处理应用程序的一些实时方面。
通过让 Nodejs 写入数据库并让我的 Rails 服务器从中读取数据,我已经能够在 Nodejs 和 Rails 服务器之间进行单向通信。
现在我想做另一种方式,即 Rails 中的操作将触发 Nodejs 中的操作。显然,我可能很愚蠢,并且有一个节点不断轮询数据库服务器。
我有什么选择?
- 在两者之间设置 RPC 调用
- 双向设置 TCP 套接字
有更简单/更快的选项吗?
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.
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.
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.
What are my options?
- Set up RPC calls between both
- Set up a TCP socket both ways
Are there easier/quicker options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,从技术上讲,你有很多进程间通信的方法,如果你想要一些简单的东西,我相信你应该看看 dnode 它在 TCP 或命名管道上提供 RPC,它有一个 Ruby 实现。让您可以轻松进行 RPC 调用,并且由于它是 TCP,因此您可以跨机器使用它。
您还可以拥有一个消息队列,例如 zeromq,但我相信这会产生不必要的开销。对于有两个以上进程相互通信的情况来说,这将是一件好事。
除了所有这些,如果你想要最小的延迟,如果你的进程都在一台机器上运行,我相信你应该使用命名管道和stdio进行通信,但我不知道节点中的任何模块可以帮助你对此进行抽象,并且您必须在 stdio 上构建自己的 RPC 模块。
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.
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.
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.
如果你使用的是redis,我最近写了一个redis_message_capsule:
来执行此操作。请随意尝试或根据需要进行修改。
If you are using redis, I recently wrote a redis_message_capsule:
to do this. Feel free to try it out or modify it as you like.