Reverse AJAX? Can data changes be "PUSHED" before the script?

I noticed that some of my ajax-heavy sites (which I visit, and not the ones I created) have certain automatic update features. For example, in GMail, if I get a new message, I see a new message without reloading the page. The same thing happens with a browser-based IM client. From what I can say, there are no java applets handling browser-server bindings, so I can only assume that this is done by AJAX and possibly some element that I don’t know about. Therefore, in my opinion, this is done in one of two ways:

  • javascript does a persistent ping on the server side script, checking for any updates that might be available (which explains why some of these pages bring any other pages of increased complexity to bypass). or

  • javascript is sitting idle, and the server side of the script actually pushes any updates to the browser. But I'm not sure if this is possible. I would suggest that there is some kind of AJAX function that still works, but all this just asks “any updates”? and the server script has a simple boolean expression that says "nope" or "I'm glad you asked." But if so, any data changes should have called the script directly so that they are ready for data changes and made changes to this logical function.

So maybe / maybe / how does it work? I imagine something like:

Someone sends an email / IM / DB update to the server, the server calls the script using the script URL plus some corresponding GET variable, the script marks this change and updates the "available updates" variable, AJAX gets the answer that there really is updates, AJAX performs its usual function of an “update page”, which executes ordinary update scripts and displays them in a browser.

I ask because it seems really inefficient that js just does a constant check that requires: a) the server to work every 1.5 seconds, and b) my browser should work every 1.5 seconds, so at my end I can say: "Oh boy, I have IM! just like a real IM client!"

+6
ajax asynchronous server-side server-push
source share
2 answers

Read about Comet

+6
source share

I really worked on a small .NET application using Ajax with the long polling method described.

Depending on what technology you are using, you can use thread signaling mechanisms to store your request until an update is received. With ASP.NET, I run my server on the same machine, so I keep a reference to my Producer object (which contains the stream that processes the data). To initiate data pushing, the Subscribe service method is called, which creates a Consumer object that is registered with Producer. If the "Consumer" operates in continuous polling mode, it has an AutoResetEvent, which is signaled whenever it receives new data, and whenever the web client makes a request for data, the consumer first waits for the reset event, and then returns it.

But you mention something about PHP - as far as I know, saving is saved through serialization, not storing the object in memory, so I don’t know how you could reference the Producer object using $ _CACHE [] or $ _SESSION []. When I developed in PHP, I never knew anything about multithreading, so I did not play with it, but I think you can learn this.

Using infinite loops will consume most of your processing power - first I will exhaust all other options.

0
source share

All Articles