聊天应用程序 - 最佳方式[关闭]

I'd like my website to have chat/group chat functionality. Site is built using php/mysql.

I have implemented client side chat screen. I am fairly good with JavaScript/jQuery and AJAX. When user enters message it is saved to the database using AJAX call. When servers responds with success I update chat window with the message user just entered.

Now the problem is here. Suppose two persons A and B are chatting. A enters a message, it is saved into database and chat window is updated. Now how come B will know that A has sent a message.

Solution I can think of is B need to "poll" server continuously after 1 or 2 seconds. But for some reason I don't think that's the right/best approach.

Is "polling" the server a best way or is there certain other approach that I don't know of?

I am ready implement this(chat app) in some other language/technology also, only thing is it should work on my php site.

I just want to know what is the best way to design this chat app for my PHP website.

P.S. please don't suggest any third party chat libraries. I am ready to code whatever it takes to build it.

Polling

This is the only way if you're using http and javascript. Http (and ajax) is single directional, the client always initiates the interaction with an action.

The only way to implement it without polling is to use a third party plugin (flash/silverlight/java) which allow a socket connection or go to a native application outside the browser.

Edit: I wasn't aware of http push, however I'm not convinced it is a practical approach to the problem.

http://ejohn.org/blog/streaming-http-server-push/

If you are comfortable with JavaScript I think you should take a look at Node.js. There's even a good example of a chat written in node (which you can browse the code on github) here.

Even if you prefer doing the server side in PHP, I think the client side of this implementation may be a good guiding example.