Is AJAX constantly looking for a server update? Javascript

I have a situation where the display on a web page needs to be updated at random. I want to do this in AJAX, but I don’t know how to do it, except how to do it

while(true) { ajaxFunction(); sleep(1) } 

Enter the text.

The problem is that the web page needs to be updated quickly when the server changes, but changes can occur very sporadically, sometimes never.

EDIT: this is an Iphone application using UIWebView, is it possible to use iPhone push notification to interact with javascript?

Thanks!

+3
source share
4 answers

I think you are describing Comet and there are several plugins for jQuery:

http://code.google.com/p/jquerycomet/

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

+6
source

The only way I can think of is to constantly request it. Just keep your answers small so you don't move a bunch of data for no reason. You can even pause the response from the server until the data is available.

 setInterval(function(){ $.get("updates.php", function(result) { alert(result); }); }, 5000); 

It is even possible to build some logic in updates.php to cancel setInterval () after 10 minutes of inactivity in some users. This will kill persistent requests from users who are no longer logged in.

+1
source

You should consider introducing some kind of comet technology (server push) when you want to optimize server load. If you have only a few users, what is the survey suitable for.

About comets: comet technology is nothing but a simple HTTP request to the server, where the server does not respond immediately, but waits until there is something to answer. Until the flow on the server is suspended.

There are some technical aspects that should be considered when implementing push server technology (for example, where should I pause a stream). It is best to use open source. It is easy to find them on the Internet if you are looking for a comet.

+1
source

If the changes are sporadic, do not use AJAX. Wait for the user to refresh the page or go to the page.

0
source

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


All Articles