How do real time updates work?

我的未来我决定 提交于 2019-11-28 16:22:40

Stack Overflow is using Web Sockets for real time updates. If you take a look in the source code (2012 source code), you would see:

StackExchange.ready(function () {
    StackExchange.realtime.init('ws://sockets.ny.stackexchange.com');
    StackExchange.realtime.subscribeToInboxNotifications();
    StackExchange.realtime.subscribeToReputationNotifications('1');
});

But note that some Opera versions do not support WebSocket. (not until Opera 10.70)

However Facebook does not seem to be using Web Sockets, and I think they are just using simple XHR with a technique called long polling, which the server holds on to the connection until there is new information, and then respond to the request. If you open up the developer tools you can see that there is always one request which has a status of pending.

It is indeed, sending a request every ~60 seconds.

It seems that Twitter also uses simple XHR (1 minute intervals) for their "real time updates".

Facebook uses long polling/Comet. So it makes a connection and waits for a response, if no response, then it times out and tries again. The timeout is around 40 secs. That's how it does most of the instant updating. However they use a combination of techniques. More on long polling here.

http://en.wikipedia.org/wiki/Comet_(programming)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!