简体   繁体   English

WebSockets和Heroku:将数据推送给用户

[英]WebSockets and Heroku: pushing data to the user

I am build a real time web application using Ruby on Rails and Heroku seems to be the best option for hosting it. 我使用Ruby on Rails构建了一个实时Web应用程序,而Heroku似乎是托管它的最佳选择。

I would prefer pushing new data to the user, when it becomes available, instead of pulling it by sending AJAX requests every few seconds. 我希望在用户可用时将新数据推送给用户,而不是每隔几秒钟发送一次AJAX请求。

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. Pusher似乎被Heroku建议用于此类工作,但它有一些限制,带来额外的成本,并使您依赖外部API。

Is there any other option to use WebSockets on Heroku? 在Heroku上还有其他选项可以使用WebSockets吗?

如果您只需要单向通信,请尝试使用服务器端事件。

There are two options that you can use with Heroku aside from Pusher: 除了Pusher之外,Heroku可以使用两个选项:

1) Like Eric Fode mentioned above, you could use SSE given you don't need two-way communication. 1)与上面提到的Eric Fode一样,您可以使用SSE,因为您不需要双向通信。 There is a Heroku add-on which you could leverage for this: https://addons.heroku.com/eshq 有一个Heroku附加组件,你可以利用它: https//addons.heroku.com/eshq

2) There is another lesser known library called Faye which you could look into. 2)还有另一个鲜为人知的图书馆叫做Faye你可以看一下。 It supports both Node.js and Ruby servers and it's based on the Bayeux Protocol 它支持Node.js和Ruby服务器,它基于Bayeux协议

If you wanna do websockets you need to use a different server besides rails. 如果你想做websockets,你需要使用除rails之外的其他服务器。 And since your on heroku you don't have the flexibility. 因为你在heroku上你没有灵活性。

Optionally you can host a websockets enabled node server on an ec2 micro instance. (可选)您可以在ec2微实例上托管启用了websockets的节点服务器。 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. 然后在你想要推送的rails应用程序中 - 向节点服务器发出请求,它将转到客户端。

虽然目前不支持WebSockets, 显然你可以在Heroku上使用Socket.io ,将其配置为长轮询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM