高效重载数据/将数据从服务器推送到客户端

2022-01-07 00:00:00 stream client-server php javascript comet

在将数据从 Web 服务器重新加载到前端时,我正在寻找可行的方法"(即最有效、最常用、最普遍接受的方法).在最终的应用程序中,我将有几个必须写入数据的输出字段,例如:

I'm looking for the 'way to go' (i.e. the most efficient, most used, general accepted way) when it comes to the reloading of data from a web server to a front end. In the end application, I will have several output fields where data has to be written to, for example like this:

在最终应用程序中,数据流将彼此不同.必须使用来自服务器的最新数据重新加载线路.

The data streams will be different from each other in the end application. The lines will have to be reloaded with fresh, up to date data from the server.

我一直在考虑使用 Ajax 请求以每秒更新一次,但必须有其他方法来做到这一点.Ajax 请求会造成大量的数据流量.此外,在使用 Facebook 聊天时,您不必每一秒都在等待,几乎可以立即收到聊天内容.然而,当我使用 Mozilla Firefox 的开发人员工具时,我没有看到任何 Ajax 轮询请求.这让我开始思考是否会有不同的方法来做到这一点.

I have been thinking of using Ajax requests to update like every second, but there has to be an other way to do this. Ajax requests will cause a lot data traffic. Also, when using the Facebook chat, you don't have to wait every second, chats are received almost instantly. Yet I don't see any Ajax polling requests being made when I use the developer tools of Mozilla Firefox. This made me think if there would be a different way to do this.

我已经研究过 Node.js,但我的主机似乎无法实现.

I've looked into Node.js, but it appears that isn't possible with my host.

我听说有人在谈论 Ajax Push,我应该使用它吗?如果是这样,你能给我一个基本的使用例子吗?

I have heard people talking about Ajax Push, is that what I should use? If so, can you give me a basic usage example?

如果不是,那么然后在具有必须在一秒钟内重新加载的多个数据流时要走的路是什么?

If not, what would then be the way to go when having multiple data streams that have to be reloaded within a second?

要求是速度和低数据流量.因此,我认为连续轮询服务器不是一种选择,因为这会产生巨大的开销.

Requirements are speed and low data traffic. It therefore wouldn't be an option to continuously poll the server, I think, because that would create an enormous overhead.

我认为这并不重要,但我在后端使用 PHP5.3,在前端使用 JavaScript 和 jQuery 1.9.1.

推荐答案

这个问题已经被问过很多次了,但方式略有不同.以下是一些值得一读的参考资料:

This question has been asked a number of times, but in a slightly different ways. Here are a few references that are worth a read:

  • 什么是长轮询、Websockets、服务器发送事件 (SSE) 和 Comet?
  • 在 PHP 中使用 Comet?
  • 支持 Comet 的 Apache
  • 服务器可扩展性 - HTML 5 websockets vs Comet
  • 如何在 PHP 中实现事件监听

总结:如果您正在考虑在 Apache 上使用 PHP 构建您的解决方案,那么保持开放的持久连接(HTTP 长轮询或流媒体)将很快耗尽资源(效率非常低)).因此,您最好使用 托管解决方案(*免责声明 - 我为托管解决方案工作).

In summary: if you are looking at building your solution using PHP on Apache then holding open persistent connections (HTTP long-polling or streaming) is going to use up resources very quickly (is highly inefficient). So, you would be better using a hosted solution (*disclaimer - I work for a hosted solution).

HTTP-Long polling 和 HTTP Streaming 是已被 Server-Sent Events 和 WebSockets 取代的解决方案.因此,在可能的情况下(在 Web 客户端提供支持的情况下),您应该在回退到基于 HTTP 的解决方案之前使用这些解决方案之一.一个好的实时网络技术会自动为您处理这个问题.

HTTP-Long polling and HTTP Streaming are solutions which have been superseded by Server-Sent Events and WebSockets. So, where possible (where the web client provides support) you should use one of these solutions before falling back to an HTTP-based solution. A good realtime web technology will automatically handle this for you.

由于您的图表显示您订阅了多个数据流,您还应该考虑 发布/订阅解决方案自然适合于此.同样,一个好的实时网络技术解决方案将为您提供这一点.

Since your diagram shows you are subscribing to multiple data streams you should also consider a Publish/Subscribe solution that naturally fits with this. Again, a good realtime web tech solution will provide you with this.

另请参阅实时网络技术指南.

相关文章