Wern Ancheta
Wern Ancheta

Reputation: 23297

How do facebook update content when somebody has posted something

I'm really interested on how facebook loads only content when someone else has posted something. The only thing that I can think of is using something like the one below to constantly update the page without reloading the page.

setInterval(ajax_stuff, 1000);

I was watching the console and indeed the request occurs and another new content is added to the page.

enter image description here

I want to be enlightened on how is this done. It would really be awesome if I can use this on a project. I mean doing setInterval every second really consumes much resource. Making a request only when its needed would be the best way to do things. Specifically I want to use it on this project:

https://github.com/anchetaWern/ChatRo

It's basically just a chat box, currently it still uses the setInterval(). I want to update only the content when someone else on the chat session has actually entered something.

Upvotes: 4

Views: 375

Answers (2)

Ryan
Ryan

Reputation: 1888

If you're looking for something on your own (non-Windows) server to the end user then also check out the APE project. It is OpenSource using SSJS using comet.

Note that it still uses Apache and your normal LAMP set-up so no need to get a dedicated machine or VPS to host it on.

I also don't know how Facebook does it but APE does sound like a good option.

I only found this with a minute or so of Googling as your question got me interested, I haven't used or tested it but as a free tool that is on your own server it definitely is worth looking at.

Also as a note, I did find a comment in Portuguese that complains that it doesn't work with Windows servers, this is specified in the documentation.

Good luck and I would be interested in how it goes if you decide to give it a shot!

Upvotes: 1

gahooa
gahooa

Reputation: 137262

I cannot speak directly to how FaceBook does this, but in general, you should be looking at WebSockets.

WebSockets allow the JavaScript on your page to maintain an open connection with a server whereby you can push data out in near-realtime to all of the clients connected to the server.

Take a look at http://pusher.com

Also, google Web Sockets.

Upvotes: 2

Related Questions