10

I am just starting to look at JQuery; until now everything has been PHP.

Just curious: if the server detects an event and wants to update the user's browser, can I do server push, or does the client have to poll?

1
  • 1
    There is no reliable pushing method currently. Take a look at long polling. Commented Nov 18, 2010 at 4:01

5 Answers 5

12

Client has to poll, but you can do long polling, i.e. keep the request alive until the server has an event to push back (i.e. complete request).

Otherwise, you can use Web Sockets.

Sign up to request clarification or add additional context in comments.

3 Comments

Got me this time. Dammit! +1 :)
And client can make other server requests during long poll? And can cancel it if need be?
@Mawg Client can make more requests, and abort existing ones. Maybe a friendly rivalry that I am about to lose!
5

The HTTP protocol works on the request-response principle which means that the server can only generate a response following a request from the client. This means that the server cannot send a response to the client without having received a request in the first place. This is not a PHP issue, it is an HTTP issue. So no, you can't push, the client has to make a request, or poll. You could take a look at long polling, as alex suggested.

1 Comment

+1 Good point; at the bottom it's all HTTP, no matter which programming language
3

You can have the client using a long-polling mechanism like comet, etc, but there's no way to genuinely "push".

1 Comment

what about websockets in PHP?
3

You can use "comet" to do this. PHP is a terrible language to do Comet in, though. One of the more popular techniques to do Comet in PHP (that sort of works) is long polling.

The idea with long polling is to create an AJAX request to the server. The server accepts the connection but doesn't respond (i.e.: a while loop with a sleep(1) in it) until an event takes place. This could be seconds, minutes, etc.

In order to make long polling "work", though, you're going to have to make sure the connection doesn't time out very quickly, so set your execution time high (minutes, or unlimited if possible). You're also going to need to write code on the client that handles the server's disconnection/timeout. When that happens, a new request should be started.

Hope this helps!

1 Comment

+1 Thank you very much for a an informative & detailed answer. "PHP is a terrible language to do Comet in" - what do you recommend?
3

That's not something really too related to jquery, but to Http itself.

It's basically not possible for a server to push anything to client actively, two possible solution is:

  1. Keep the Http Connection without closing it.

  2. Polling

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.