问题
I am developing a news feed module for my web project. News feed activities (post, filter, etc.) are nearly similar to Facebook. I ve used pagination pattern that initializes news page by page (eg: 20 post for every scroll) when user scrolls the page down (unlimited scrolling).
I wonder how Facebook initializes current news when one of your friends shares a post. I guess it uses a trigger that sends an ajax request to get if new posts are exist. Using a timer trigger (with javascript timeout function which sends ajax requests every 10 seconds) would not be an effective solution for this problem.
Does anyone have any other trigger advices for me ?
Thanks in Advance.
回答1:
If you were to monitor the network activity of the home page, you'd see calls to https://pct.channel.facebook.com/pull
with some unique parameters attached to it. While watching it you'd see that the calls take a variable length of time, from 1 second to ~60 seconds.
This is referred to as Long-Polling, where the server waits until new information is available to send back a response. Meantime, the HTTP call is held in suspense as though the endpoint is loading. Once the server finds information available, it sends a response with the data, and closes the connection. Then the client re-opens the connection with another HTTP call once it receives data.
来源:https://stackoverflow.com/questions/12723403/how-facebook-initializes-recent-news-feed-entries-instantly