2

I am simulating push notifications using PHP the following way:

  1. A jQuery ajax call calls a script on the server.
  2. The script is being delayed using a for loop and a sleep after each iteration.
  3. If something happens - the loop is being breaked and the information returned to the jQuery.
  4. If the script runs for more than one minute - the script returns an empty value.
  5. When the jQuery receives an answer from the server it parses the information and starts the same procedure again.

That's a procedure, used by facebook and it works like a charm on their website. On my server I have the following problem: For example if the script is being delayed for 60 seconds and I click on another link on my website on the 30-th second we still have 30 seconds left to generate the output. So my webserver is waiting those 30 seconds and my click request is being processed after those seconds, making my website almost impossible to use. I have doublechecked facebook and I found that, when you click on a link the page is never refreshed and the requests keep flowing on the same page. The adressbar however changes. What is the way to achieve the same thing on my website? Is there a way to force my server to process more then one PHP request at a time or we have to do this using javascript only. What am I missing?

P.S as far as I know there is no way to change the adressbar using javascript.

  • I understood the concept, but wouldn't polling be more effective? This way you can wait on the client, not on the server. You are effectively doubling the connections your server get to handle. – Spidey Apr 19 '12 at 12:52
  • 2
    for the "change address bar using js " http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page – Tarek Apr 19 '12 at 12:54
  • If by polling you mean 'the client to ask the server for changes every several seconds' it's a little more unstable, because each query is being wrapped with HTTP headers and it takes longer to transfer the data. We will also have more then 1000 users on this web site at a time, so imagine what will happen to the server if 1000 users are asking every several seconds for changes. – Дамян Станчев Apr 19 '12 at 12:56
  • @Spidey technically, he is doing polling, but not the interval kind. He's holding the connection, not letting the server respond until something happens or reaches the timeout. if a response is sent, the JS parses the return and calls the server for another "held connection". – Joseph Apr 19 '12 at 12:57
  • Yeah, I understand this, but the server needs to handle lots of simultaneous connections. If the http header overhead is a problem to you, that's a better solution, but I think server memory/processor would be an issue with so many open connections at a time. While a standard connection would take less than a second for the server to process, this kind of connection would hang for a longer time. – Spidey Apr 19 '12 at 17:54

1 Answers1

2

Actually there is in HTML 5. It's called History API.

You could find a quick demo with the source code on GitHub here:

http://html5demos.com/history


The complete chapter about it in Dive Into HTML5:

http://diveintohtml5.info/history.html

Vladimiroff
  • 473
  • 4
  • 11