I am no expert on this, but I did have a little look into it a while ago.
From what I can see there are two ways to go about this. The first (easy but a bit crap) is to have some form of refreshing going on. With each refresh you simply look to see if there are any new message to display. As I said... easy but a bit crap.
The other way is to use a socket server, which I think is probably what you are looking to do. It is apparently possible to write these in PHP, but definitely beyond me at this time.
Try taking a look at Unity (http://www.moock.org/unity/). I seem to remember that this was about the best for what I was looking to do at the time. I believe that there is a free version as well that is scaled down, but should be fine for chat.
Hope that this is some help.