How to transfer new HTML data from the server to the browser without refreshing the page

I saw a lot of sites (like facebook or stack overflow) that will update some functions as new data is created (like a new message reply).

Suppose that a new post has been added to the blog, and someone on the blog is looking at it at that moment, the idea is to automatically add a new post to the blog without updating, or be able to do something.

I was thinking of making an AJAX call every 5 seconds or so, but that would cause too many requests to the server, and I saw that the sites I mentioned (which update the content) did not create any new requests.

I am really out of ideas on how to achieve this. Is this possible with PHP and AJAX? I also heard about websockets. Any ideas? Thanks

+4
source share
4 answers

Great question! There are several solutions.

A relatively new solution has emerged for these call sites. This is in the HTML5 standard and there are many libraries for it in other languages.

One of them is sockets.io (javascript) - it allows you to maintain a serial connection with a low latency to the server to receive information as it is created. There are also many plugins for php, c and python for the server side.

To name a few: libwebsockets (C ++), pywebsockets (python) and jetty (javascript)

And for php: http://socketo.me/

Visit this link for more information: http://www.html5rocks.com/en/tutorials/websockets/basics/

+5
source

You can use HTML5 SSE . But this is not a good solution.

You can use the Comet server, which helps in polling Long AJAX. Check out the Ajax Push Engine (APE) . This is a combination of Comet Server and a Javascript framework for creating AJAX-PUSH or real-time streaming.

Hooray!:)

+2
source

One example of a framework that can help you is SignalR http://signalr.net/

This is a specific asp.net though

+1
source

Well,

If you are in the world of PHP and Open Source, you can consider Node.Js, Socket.IO or NowJs

I am in the wonderful world of ASP.Net and I love SignalR.

0
source

All Articles