Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP based Chatroom

Where to find a good tutorial

10:09 pm on May 29, 2008 (gmt 0)

Full Member

10+ Year Member

joined:Dec 12, 2007
votes: 0


I used to see some chats on some websites and I am wondering if this can be done with PHP.

Can someone point me to a good online tutorial?

Thanks in advance

10:23 pm on May 29, 2008 (gmt 0)

Full Member

10+ Year Member

joined:Feb 14, 2005
posts: 266
votes: 0

As in forum? Don't reinvent the wheel:


10:34 pm on May 29, 2008 (gmt 0)

Full Member

10+ Year Member

joined:Dec 12, 2007
votes: 0

I meant a tutorial where I can learn to write the php program running a chat, with all the info of number of people chatting ,etc...
6:52 pm on May 30, 2008 (gmt 0)

Full Member

10+ Year Member

joined:July 13, 2007
votes: 0

I've developed something like this in the past, and it worked pretty well. It was a CodeIgniter library, actually.

I can tell you that you'll need to know how to use AJAX, and have a library that handles AJAX calls efficiently and quickly. The YUI library does wonders in helping the client-side development, and its documentation is excellent. I would recommend using JSON encoding instead of XML no matter what solution you pick, as in a chat system you will need to have a request/response several times per minute for every user, and XML as a transmission mechanism is amazingly inefficient compared to JSON, especially for empty requests (I.E. no new messages have been sent since the last request).

The tricky part in building this sort of a system is that only the client can initiate communication with the server. In a normal instant-messaging scenario, the server initiates communication with the client, telling the client that a new message is available, then transmitting it.

You'll have to use a timed event (setInterval in Javascript works wonderfully for this) on the client to repeatedly ping the server to request recent messages. This "ping" should probably include the time at which the last ping occurred (or at least a cookie that allows you to store this information in the session), so it can retrieve messages occurring since then from the database.

Your server-side code will have to be lean and efficient--you won't have the luxury of spending even a half second processing the request if you want to have the chat interface seem responsive. Also keep in mind that the sheer volume of requests coming in at the same time can potentially be crippling with a large processing time if there are even 50 users online at any given time.

To my knowledge there are no tutorials available for building this sort of thing--they'd have to be long and involved, really, in order to have a prayer at being successful.

You'll need a solid knowledge of the following technologies to complete a web chat system:
CSS (inline HTML styling will use up so much bandwidth in transfers that your system -will- grind to a halt)
YUI (or other AJAX-capable library)

PHP has a wonderful json_encode() function that can encode any variable type into JSON, but it only comes bundled and enabled by default with PHP 5.2 or newer. Between this function and the YUI library (which includes a JSON decode function) your transmissions will be all but handled for you.

11:56 am on June 1, 2008 (gmt 0)


WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
votes: 2

Can someone point me to a good online tutorial?

Zend put out a Ajax Chat Tutorial [devzone.zend.com] awhile ago that may help you get started.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members