[RESOLVED] [jQuery & PHP] Long polling or Comet programming in Chat ?
Hi guys :wave:
I have developed a chat application using PHP and jQuery. So, at the moment, my chat app would poll the server every 2.5 seconds, with the last message's id as GET request. And in the server side, the script will establish a db connection and check whether any new messages are arrived which is greater than the message_id that was received through GET request.
If found, echo the new messages. Otherwise, echo empty string. So far it works okay.
But I have heard that we could make the polling efficient by using a long polling method or a comet programming method. Based on the my research(search done on Google :D), I found that comet is not cross-browser supporting. So, I thought about the long polling mechanism. But what it does is, it would use a loop in the serverside, checking for new data arrivals and if so, echo it. Otherwise, stay in the loop until new messages comes.
But wouldn't it eat server resource ?
Do you think long polling would be the best ? Or do you know some other technique which is more efficient ?
Thanks :wave:
Re: [jQuery & PHP] Long polling or Comet programming in Chat ?
Quote:
Originally Posted by
akhileshbc
But what it does is, it would use a loop in the serverside, checking for new data arrivals and if so, echo it. Otherwise, stay in the loop until new messages comes.
But wouldn't it eat server resource ?
This is a technique I have used in the past and it worked reasonably well, although I agree it is a little intensive on the server side. A (possibly) better option would be to use sockets to communicate between the database and listener processes on the server side, rather than constantly polling the database. But I'm not sure whether you would see much or any benefit from this, and unless you were running a very busy server I'm certain it would not be worth the effort. I would stick with the 'Comet' (long poll) model myself.
Re: [jQuery & PHP] Long polling or Comet programming in Chat ?
Re: [RESOLVED] [jQuery & PHP] Long polling or Comet programming in Chat ?
Just another doubt: I'm now staring at this code - http://lxcblog.com/2010/10/17/jquery...aging-example/
So, that will poll the server script for a new message and the server will loop until a new message is found.
But what happens if we want to post a message to that same PHP page ? :confused:
Isn't it still busy with the loop ?
Edit:
I think, I got it. When a PHP page is requested, it would create an instance of it. When another one request the same page, a new instance will be used. And at the end of the script, the current instance will be disposed. Isn't it ? I'm confusing my mind by mixing desktop and web programming ! :)
Re: [RESOLVED] [jQuery & PHP] Long polling or Comet programming in Chat ?