从服务器轮询数据 - 最effecient实用的方法/设置 [英] Polling data from server - most effecient and practical way/setup

查看:114
本文介绍了从服务器轮询数据 - 最effecient实用的方法/设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读几个帖子就到这里就投票,甚至有一起来看看虽然我不'吨要往下走这条路线,并需要作出efficent通报制度方面的一些建议。如何Facebook,Twitter和其他网站做到这一点?他们使用的网络套接字?

解决方案

查询

>从服务器轮询数据 - 最 >高效,实用的方法/设置

您应该避免投票,因为它是没有效率也没有实时的。比方说,你轮询每隔30秒,您的服务器可以处理负载,没有任何问题。问题就在于你的数据不是实时的。你可以缩短轮询间隔(每秒),但你将有一个非常困难的时期试图扩展你的服务器。

但有时轮询(智能)也很不错,因为它很容易实现。一些技巧我对你有:

  • 不要使用数据库,但是从内存数据库如 Redis的检索数据的memcached 的,因为它们是要快得多。这是秘方最流行的大玩家(网站)平稳运行。 Facebook有使用大量内存使用memcached的专用服务器(5 TB在2008 => Facebook已经成长了很多,因为;))

    如果您不能安装(也许应该!)的Memcached或Redis的服务器上,你可以考虑使用托管 http://redistogo.com 这是免费的小型网站。

  • 其他的事情要做的就是增加使用GitHub的库以$ P轮询间隔$ pvent服务器过载。


>如何Facebook,Twitter和其他 >网站做到这一点?他们使用的网络套接字?

网络插座

一些网站正在使用的WebSockets,但是这只是众多1运送他们的支持,因为WebSockets的不是在所有浏览器。在将来,当所有的浏览器支持的WebSockets,就可以使用的(可能)的唯一传输。下面我会给你所有流行的传输器列表,用简单描述:

  

的WebSocket是一个技术提供   双向,全双工   通信信道,在一个单一的   传输控制协议(TCP)   插座。它被设计成   在浏览器和网络实施   服务器,但它可用于通过任何   客户端或服务器应用程序。

     

有关客户端,WebSocket的是   在Firefox 4,谷歌执行   镀铬4,歌剧11,和Safari 5,作为   以及Safari浏览器的移动版本   在iOS 4.2的。然而,虽然   present,支持现在被禁用   默认情况下,Firefox和Opera,因为   过安全问题   漏洞。

  

有关的大部分,XMLHtt prequest长   投票站的工作原理就像任何标准的使用   XHR。浏览器发出一个异步   服务器的请求,其可以等待   用于数据可用之前   响应。

这交通工具可在每一个浏览器。

  

一个长轮询的Comet运输均可   通过动态地创建脚本创建   元件,并且它们的源设置为   彗星服务器的位置,   然后发送回的JavaScript(或   JSONP)的一些事件作为其有效载荷。   每次脚本请求   完成后,浏览器打开一个新的   之一,就像在XHR长轮询   外壳。这种方法的优点是   是跨浏览器,同时还   允许跨域实现。

  

提供一个可用的流媒体传输   在Internet Explorer

  

的XMLSocket是一类在ActionScript   这使Adobe Flash内容   使用socket通信,通过TCP   流式套接字。它可用于   纯文本,虽然,正如其名称   顾名思义,它是为XML。它是   经常在聊天应用程序使用,   多人游戏。

正如你可能知道Facebook并使用PHP有积极的发展,但实际上并不使用它的任何那里有现场实时元素,因为PHP是不是设计来处理这个正确的(没有?)。很多人感到恼火,我要这样说,但我不能帮助它是真理(甚至是Facebook的同意)。在PHP中,几乎所有的函数调用(C)使用非阻塞I / O使扩展的实时系统几乎是不可能的。我读了一篇博客文章在这里使用非阻塞IO用PHP (质量?)。在过去的Facebook使用二郎这也是pretty的流行做非阻塞IO创建聊天。我自己觉得二郎神code看着奇怪,但我还是想了解它。以下是关于Facebook的使用Erlang的一些链接:

  • <一个href="https://www.facebook.com/note.php?note_id=91351698919">https://www.facebook.com/note.php?note_id=91351698919
  • <一个href="https://www.facebook.com/note.php?note_id=51412338919">https://www.facebook.com/note.php?note_id=51412338919
  • <一个href="http://www.infoq.com/news/2008/05/facebookchatarchitecture">http://www.infoq.com/news/2008/05/facebookchatarchitecture

另外Facebook的收购,友在过去和开源还有的这是用Python编写的做龙卷风框架非阻塞IO。

  

它不再只是传统的   Linux操作系统,Apache,MySQL和PHP栈   这使得像Facebook或网站   FriendFeed的可能,但新   像龙卷风基础设施工具,   卡桑德拉,蜂巢(建立在顶部   Hadoop的),内存缓存,抄写员,节俭和   其他都是必不可少的。我们相信,在   释放有用外   基础设施组件作为开放   源(见的Facebook开源)作为   方式来增加通过创新   Web上。

我想他们也使用龙卷风因为有系统的某些部分了。


其他网站

下面我将尝试列出一些流行的框架(开源)做非阻塞IO:

  • Socket.io (Node.js的):Socket.io正在成为pretty的流行非阻塞的node.js的框架( 1722人正在看这个项目在Github现在)。我真的很喜欢Socket.io
  • 的Netty (JAVA):该Netty的项目是为了提供一个异步事件驱动的网络应用框架和工具,用于快速开发维护的高性能和放大器;高扩展性协议服务器和放大器;客户端。
  • 龙卷风(蟒蛇):龙卷风是可扩展的,无阻塞的网络服务器的开源版本和工具,权力的FriendFeed。

&GT;甚至有一个看看推,虽然我 &GT;不愿走这条路线

我不明白你不喜欢推,因为它是一个pretty的流行托管解决方案。有了这个托管解决方案,你可以开始而不pretty的良好定价任何麻烦构建可扩展的实时服务(免费的,如果小和pretty的负担得起的中间范围的网站)。

I've been reading a few posts on here regarding polling and even had a look at Pusher although i don't want to go down that route and need some advice in regards of making an efficent notification system. How do facebook, twitter and other websites do this? are they using web sockets?

解决方案

Polling

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

You should avoid polling because it is not efficient and also not real-time at all. Let's say you poll every 30 seconds and your server can handle the load without any problems. The problem then is that your data is not real-time. You could shorten the poll-interval (every second), but then you will have a very difficult time trying to scale your server.

But sometimes polling (smart) is also very nice, because it is easy to implement. Some tips I have for you are:

  • Don't use the database, but retrieve data from in-memory database like redis, memcached because they are much faster. That is the secret ingredient for most popular big players(websites) to run smoothly. Facebook has special purpose servers that use a lot of memory using memcached (5 TB in 2008 => Facebook has grown a lot since ;)).

    If you can't install (probably should!) Memcached or Redis on your server you could consider using the hosted http://redistogo.com which is free for small sites.

  • The other thing to do is increment the poll interval using github's library to prevent server overloading.


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

Web-sockets

Some of these sites are using websockets, but that is only one of the many transports that they support because websockets aren't available in all browsers. In the future when all browsers support websockets that will be the only transport used (probably). Below I will give you a list of all the popular transports with a quick description:

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.

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.

This transport is available in every browser.

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.

provide a usable streaming transport in Internet Explorer

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

As you probably know Facebook does use PHP for there active development, but actually don't use it for any of there real-time elements on there site, because PHP is not designed to handle this properly (yet?). A lot of people get mad at me for saying this, but I can't help that it is the truth (even Facebook agrees). In PHP almost all function calls (C) are using blocking I/O which makes scaling real-time systems almost impossible. I read a blog post over here using non-blocking IO with PHP (quality?). In the past Facebook created the chat using Erlang which is also pretty popular for doing non-blocking IO. I myself find the Erlang code looking strange, but I still would like to learn it. Here are some links about Facebook using Erlang:

Also Facebook bought Friendfeed in the past and open-sourced there Tornado framework which is written in Python to do non-blocking 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.

I assume they are also using tornado for some parts of there system now.


Other sites

Below I will try to list some popular frameworks(open-source) to do non-blocking IO:

  • Socket.io (Node.js): Socket.io is becoming a pretty popular non-blocking framework for node.js (1722 people are watching this project on Github right now). I really like Socket.io
  • Netty (Java): The Netty project is an effort to provide an asynchronous event-driven network application framework and tools for rapid development of maintainable high performance & high scalability protocol servers & clients.
  • tornado (Python): Tornado is an open source version of the scalable, non-blocking web server and tools that power FriendFeed.

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

I don't understand your dislike for pusher because it is a pretty popular hosted solution. With this hosted solution you could started building scalable real-time service without any hassle with pretty good pricing (free if small and pretty affordable for middle range websites).

这篇关于从服务器轮询数据 - 最effecient实用的方法/设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆