How to update facebook content when someone posted something

I'm really curious how facebook only uploads content when someone else posted something. The only thing I can think of is to use something like below to constantly refresh the page without reloading the page.

setInterval(ajax_stuff, 1000); 

I watched the console, and indeed, the request appeared, and new new content was added to the page.

enter image description here

I want to be enlightened about how this is done. It would be great if I could use this in a project. I mean, setInterval really consumes a lot of resources every second. Fulfilling a request only when it was necessary would be the best way to do something. In particular, I want to use it in this project:

https://github.com/anchetaWern/ChatRo

This is basically just a chat window, currently it still uses setInterval (). I want to update only the content when someone else in the chat session really entered something.

+4
source share
2 answers

I can't directly talk about how FaceBook does this, but overall you should look at WebSockets.

WebSockets allow JavaScript on your page to maintain an open connection to the server, through which you can output data in almost real time to all clients connected to the server.

Take a look at http://pusher.com

Also google web sockets.

+2
source

If you are looking for something on your own (not Windows) for the end user, then also check out the APE project . This is OpenSource using SSJS using comet.

Note that it still uses Apache and your usual LAMP setup, so there is no need to get a dedicated computer or VPS to host it.

I also don't know how Facebook does this, but APE really sounds like a good option.

I just found this for a minute or so in Googling, since your question interested me, I did not use or test it, but as a free tool that is located on your own server, which is definitely worth a look.

As well as a note, I found a comment in Portuguese that complains that it does not work with Windows servers, as stated in the documentation.

Good luck, and I would be wondering how this happens if you decide to give him a chance!

+1
source

Source: https://habr.com/ru/post/1400032/


All Articles