WebSockets 和 Heroku:将数据推送给用户
我正在使用 Ruby on Rails 构建一个实时 Web 应用程序,Heroku 似乎是托管它的最佳选择。
我更喜欢在新数据可用时将其推送给用户,而不是通过每隔几秒发送 AJAX 请求来拉取它。
Heroku 似乎建议使用 Pusher 来做这样的工作,但它有一些限制,带来了额外的成本,并且使得您依赖于外部 API。
还有其他选项可以在 Heroku 上使用 WebSockets 吗?
I am build a real time web application using Ruby on Rails and Heroku seems to be the best option for hosting it.
I would prefer pushing new data to the user, when it becomes available, instead of pulling it by sending AJAX requests every few seconds.
Pusher seems to be suggested by Heroku for a such kind of job, but it has some limitations, brings additional costs, and makes you dependent on an external API.
Is there any other option to use WebSockets on Heroku?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果你想做 websocket,你需要使用除 Rails 之外的不同服务器。而且由于您使用 Heroku,因此您没有灵活性。
您也可以选择在 ec2 微型实例上托管启用 Websockets 的节点服务器。然后在你的 Rails 应用程序中,当你想要推送时,向节点服务器发出请求,它将发送给客户端。
If you wanna do websockets you need to use a different server besides rails. And since your on heroku you don't have the flexibility.
Optionally you can host a websockets enabled node server on an ec2 micro instance. Then in your rails app when you want to push -- do a request to the node server and the it will go to the clients.
如果您只需要一种通信方式,请尝试使用服务器端事件。
If you only need one way communication try using Server Side events.
除了 Pusher 之外,您还可以使用 Heroku 的两个选项:
1) 就像上面提到的 Eric Fode 一样,您可以使用 SSE,因为您不需要双向通信。您可以利用一个 Heroku 插件来实现此目的:https://addons.heroku.com/eshq
2)还有另一个不太为人所知的库,名为 Faye 您可以研究一下。它支持 Node.js 和 Ruby 服务器,并且基于 Bayeux 协议
There are two options that you can use with Heroku aside from Pusher:
1) Like Eric Fode mentioned above, you could use SSE given you don't need two-way communication. There is a Heroku add-on which you could leverage for this: https://addons.heroku.com/eshq
2) There is another lesser known library called Faye which you could look into. It supports both Node.js and Ruby servers and it's based on the Bayeux Protocol
Heroku 现在支持 WebSocket:https://devcenter.heroku.com/articles/heroku-实验室-websockets
Heroku now has support for WebSockets: https://devcenter.heroku.com/articles/heroku-labs-websockets
虽然目前不支持 WebSockets,但显然您可以在 Heroku 上使用 Socket.io,长期配置-轮询。
While WebSockets isn't currently supported, apparently you can use Socket.io on Heroku, configuring it for long-polling.