Forum Moderators: coopster

Message Too Old, No Replies

PHP Chat and event listeners

         

Tommybs

7:47 am on Jan 13, 2010 (gmt 0)

10+ Year Member



Hi,

I've been looking at putting together a really simple style chat room in php and AJAX and I got to thinking about event listeners. If I coded this in Flash say I could have an event listener update the chat for all users when a message is submitted as opposed to just whoever submitted this.

Would there be a way to do this in php without using a timer to constantly refresh the chat window? E.G if user1 posts a message, the php script knows a message has been posted and the view would update for users 1,2 and 3? I just can't get my head around how this could work...

vincevincevince

2:39 pm on Jan 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you can do is have your AJAX request hang, that is, keep it stuck in a loop in the PHP script before you output the response. If you keep it stuck (sleep(1);loop) until you have a message, then output the message; it will be picked up straight away.

If it has waited 20 seconds, you best send a <no message> response which starts a new request, to avoid browser timeouts.