最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Technology behind real-time polling - Stack Overflow

programmeradmin3浏览0评论

I am looking at facebook news feed/ticker right now and I am wondering what technology/architecture it uses to pull in data asynchronously when any of my connections make an update. One possibility that I can think of is a javascript setInterval on a function that aggressively polls the server for new data.

I wonder how efficient that is.

Another possible technology that I can think of is something like Comet/NodeJS architecture that pings the client when there is an update on the server. I am not too familiar with this technology.

If I wanted to create something similar to this. What should I be looking into? Is the first approach the preferred way to do this? What technologies are available out there that will allow me to do this?

I am looking at facebook news feed/ticker right now and I am wondering what technology/architecture it uses to pull in data asynchronously when any of my connections make an update. One possibility that I can think of is a javascript setInterval on a function that aggressively polls the server for new data.

I wonder how efficient that is.

Another possible technology that I can think of is something like Comet/NodeJS architecture that pings the client when there is an update on the server. I am not too familiar with this technology.

If I wanted to create something similar to this. What should I be looking into? Is the first approach the preferred way to do this? What technologies are available out there that will allow me to do this?

Share Improve this question asked Jan 4, 2012 at 9:48 dennissdenniss 17.6k26 gold badges93 silver badges147 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 12

There are several technologies to achieve this:

  • polling: the app makes a request every x milliseconds to check for updates
  • long polling: the app makes a request to the server, but the server only responds when it has new data available (usually if no new data is available in X seconds, an empty response is sent or the connection is killed)
  • forever frame: a hidden iframe is opened in the page and the request is made for a doc that relies on HTTP 1.1 chunked encoding
  • XHR streaming: allows successive messages to be sent from the server without requiring a new HTTP request after each response
  • WebSockets: this is the best option, it keeps the connection alive at all time
  • Flash WebSockets: if WS are not natively supported by the browser, then you can include a Flash script to enhance that functionality

Usually people use Flash WebSockets or long-polling when WebSockets (the most efficient transport) is not available in the browser.

A perfect example on how to bine many transport techniques and abstract them away is Socket.IO.

Additional resources:

http://en.wikipedia/wiki/Push_technology
http://en.wikipedia/wiki/Comet_(programming))
http://www.leggetter.co.uk/2011/08/25/what-came-before-websockets.html
Server polling with JavaScript
Is there a difference between long-polling and using Comet
http://techoctave./c7/posts/60-simple-long-polling-example-with-javascript-and-jquery
Video discussing different techniques: http://vimeo./27771528

The book Even Faster Websites has a full chapter (ch. 8) dedicated to 'Scaling with Comet'.

I could be wrong, but I think that Facebook relies on a "long polling" technique that keeps an http connection open to a server for a fixed amount of time. The data sent from the server triggers an event client side that is acted upon at that time. I would imagine that they use this technique to support the older browsers that do not have websocket support built in.

I, personally, have been working on an application with similar requirements and have opted to use a bination of node.js and socket.io. The socket.io module uses a variety of polling solutions and automatically chooses the best one based on what is available on the client.

Maybe you may have a look to Goliath (non-blocking IO server written in Ruby) : http://postrank-labs.github./goliath/

发布评论

评论列表(0)

  1. 暂无评论