简体   繁体   中英

Implementing facebook like real-time newsfeed ajax refresh?

The first version, I query the database every 1 second for new data. This works but this is bad, database is getting queried too many times.

How do I efficiently implement real-time ajax refresh content on the page just like facebook newsfeed?

"real-time" is best achieved using web sockets or similar techniques, where the server can post data to the client when new content is available instead of constant polling from the client.

If you need to do ajax polling, use a slower interval and make sure you cache the request on the server so you don't do any queries if there is no new data available. You can also send a token from the client (f.ex timestamp) so the server can check if the token is "fresh" or if you need to query new data before you return the response.

Why not just use RSS style feeds? You could poll the database once every minute or so to generate the feed, or you can tie it in so you can append directly to the feed from the input process?

http://www.rssboard.org/rss-specification

Once you have the feed built (and optionally secured behind your login system) you can just access it using jFeed.

http://plugins.jquery.com/project/jFeed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM