如何使用 node.js 实现实时前端更新 + Ruby on Rails?

发布于 2024-09-01 06:19:16 字数 75 浏览 1 评论 0原文

使用 Node.js 在 ruby​​ on Rails 中实现实时更新的最佳方法是什么?很高兴听到真实的例子或您对替代解决方案的想法。

What is the best way to implement real time updates in ruby on rails using node.js? It would be great to hear either real examples or your thoughts on alternative solutions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东北女汉子 2024-09-08 06:19:16

我使用 Node 与 Rails 应用程序聊天。

我的方法是设置一个 nginx 前端来代理我的 Rails 应用程序和节点应用程序。

这使您可以绕过同源政策并进行交叉通信。

这是我的 nginx.conf 的片段,

   location /chat_service {
      rewrite      /chat_service/(.+) /$1 break;
      proxy_pass http://localhost:9000/;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
    }

这意味着我可以从 Rails 应用程序渲染 html 页面并与节点应用程序通信,而无需使用 JSONP 等令人讨厌的 hack。

完整的示例超出了本答案的范围,但是在前面有一个好的代理,您可以让它们愉快地一起工作。

I use node for chat with a rails app.

The way I do it is by setting up an nginx front end that proxies my Rails app and my node app.

This allows you to get around the same origin policy and cross communicate.

Here is a snippet of my nginx.conf

   location /chat_service {
      rewrite      /chat_service/(.+) /$1 break;
      proxy_pass http://localhost:9000/;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
    }

This means that I can render the html pages from my rails app and communicate with the node app without having to use nasty hacks like JSONP.

A full example is way out of the scope of this answer, but with a good proxy in front you can get them to work happily together.

ヅ她的身影、若隐若现 2024-09-08 06:19:16

实现这一点的方法是使用像 jquery 这样的 JavaScript 框架。一旦 Rails 渲染视图并将 html 传递到客户端浏览器,JavaScript 就可以接管处理更新并从 Node.js 请求信息,因为 Node 可以处理数千个并发连接。

您可以使用此方法进行简单的 ajax 调用或形成更复杂的 comet 推送更新。

The way to do it would be with a javascript framework like jquery. Once rails renders the views and passes the html to the client's browser, the javascript can take over to handle updates and request information from node.js since node can handle thousands of concurrent connections.

You can use this method for simple ajax calls or form more complex comet push updates.

坦然微笑 2024-09-08 06:19:16

恐怕没那么简单......

因为我们有同源政策
在这种情况下,页面(由rails渲染)和更新服务器(节点)必须位于同一服务器上。端口

我仍然不知道该怎么做。

It's not that simple, I'm afraid...

cause we got the same origin policy
in that case, the page (rendered by rails) and the update server (node) must be on the same server & port

I still don't know how to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文