gpt4 book ai didi

php - 从服务器轮询数据 - 最有效和实用的方式/设置

转载 作者:行者123 更新时间:2023-12-01 13:49:16 25 4
gpt4 key购买 nike

我在这里阅读了一些关于投票的帖子,甚至查看了Pusher。虽然我不想走那条路,需要一些关于制作有效通知系统的建议。 facebook、twitter 和其他网站是如何做到这一点的?他们在使用网络套接字吗?

最佳答案

轮询

> Polling data from server - most
> efficient and practical way/setup

您应该避免轮询,因为它效率不高,而且根本不是实时的。假设您每 30 秒轮询一次,您的服务器可以毫无问题地处理负载。那么问题是您的数据不是实时的。您可以缩短轮询间隔(每秒),但是您将很难尝试扩展您的服务器。

但有时轮询(智能)也很不错,因为它很容易实现。我给你的一些建议是:
  • 不要使用数据库,而是从内存数据库中检索数据,如 redis , memcached因为它们要快得多。这是大多数受欢迎的大玩家(网站)顺利运行的秘诀。 Facebook 有使用大量 memory using memcached 的专用服务器。 (2008 年为 5 TB => 此后 Facebook 增长了很多;))。

    如果您无法在您的服务器上安装(可能应该!) Memcached 或 Redis,您可以考虑使用托管的 http://redistogo.com这对于小型网站是免费的。
  • 另一件事是increment the poll interval using github's library以防止服务器重载。


  • > How do facebook, twitter and other
    > websites do this? are they using web sockets?

    网络套接字

    其中一些站点正在使用 websockets,但这只是它们支持的众多传输之一,因为 websockets 并非在所有浏览器中都可用。将来,当所有浏览器都支持 websockets 时,这将是唯一使用的传输方式(可能)。下面我将为您列出所有流行的交通工具,并简要说明:
  • websockets :

  • WebSocket is a technology providing for bi-directional, full-duplex communications channels, over a single Transmission Control Protocol (TCP) socket. It is designed to be implemented in web browsers and web servers, but it can be used by any client or server application.

    For the client side, WebSocket was to be implemented in Firefox 4, Google Chrome 4, Opera 11, and Safari 5, as well as the mobile version of Safari in iOS 4.2. However, although present, support is now disabled by default in Firefox and Opera because of concerns over security vulnerabilities.


  • xhr long-polling :

  • For the most part, XMLHttpRequest long polling works like any standard use of XHR. The browser makes an asynchronous request of the server, which may wait for data to be available before responding.



    这种传输在每个浏览器中都可用。
  • json-p long-polling :

  • A long-polling Comet transport can be created by dynamically creating script elements, and setting their source to the location of the Comet server, which then sends back JavaScript (or JSONP) with some event as its payload. Each time the script request is completed, the browser opens a new one, just as in the XHR long polling case. This method has the advantage of being cross-browser while still allowing cross-domain implementations.


  • htmlfile :

  • provide a usable streaming transport in Internet Explorer


  • flashsocket

  • XMLSocket is a class in ActionScript which allows Adobe Flash content to use socket communication, via TCP stream sockets. It can be used for plain text, although, as the name implies, it was made for XML. It is often used in chat applications and multiplayer games.



    Facebook

    您可能知道 Facebook 确实使用 PHP 进行积极的开发,但实际上并没有将其用于该网站上的任何实时元素,因为 PHP 的设计不是为了正确处理这个问题(还没有?)。很多人因为我这么说而生我的气,但我不禁认为这是事实(甚至 Facebook 也同意)。在 PHP 中,几乎所有函数调用 (C) 都使用阻塞 I/O,这使得扩展实时系统几乎是不可能的。我在这里使用 non-blocking IO with PHP 阅读了一篇博文(质量?)。过去 Facebook 使用 Erlang 创建聊天。这在做非阻塞 IO 时也很受欢迎。我自己觉得 Erlang 代码看起来很奇怪,但我仍然想学习它。以下是一些关于 Facebook 使用 Erlang 的链接:
  • https://www.facebook.com/note.php?note_id=91351698919
  • https://www.facebook.com/note.php?note_id=51412338919
  • http://www.infoq.com/news/2008/05/facebookchatarchitecture

  • Facebook还购买了 Friendfeed过去并在那里开源 Tornado用 Python 编写的用于执行非阻塞 IO 的框架。

    It is no longer just the traditional Linux, Apache, MySQL, and PHP stack that make a site like Facebook or FriendFeed possible, but new infrastructure tools like Tornado, Cassandra, Hive (built on top of Hadoop), memcache, Scribe, Thrift, and others are essential. We believe in releasing generically useful infrastructure components as open source (see Facebook Open Source) as a way to increase innovation across the Web.



    我想他们现在也对那里系统的某些部分使用 Tornado 。

    其他网站

    下面我将尝试列出一些流行的框架(开源)来做非阻塞 IO:
  • Socket.io (Node.js):Socket.io 正在成为一个非常流行的 node.js 非阻塞框架(目前有 1722 人在 Github 上观看这个项目)。我真的很喜欢 Socket.io
  • Netty (Java):Netty 项目致力于提供异步事件驱动的网络应用程序框架和工具,用于快速开发可维护的高性能和高可扩展性协议(protocol)服务器和客户端。
  • tornado (Python):Tornado 是可扩展、非阻塞 Web 服务器的开源版本,以及为 FriendFeed 提供支持的工具。


  • > even had a look at Pusher although I
    > don't want to go down that route

    我不明白您不喜欢 pusher,因为它是一种非常流行的托管解决方案。使用这个托管解决方案,您可以开始构建可扩展的实时服务,而无需任何麻烦,而且价格非常优惠(如果规模较小,则免费,并且对于中端网站来说相当实惠)。

    关于php - 从服务器轮询数据 - 最有效和实用的方式/设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6511979/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com