Arkanis Development


A simple chat in about 50 lines of code


Ever wanted to add a small chat to your own website? If you look around you'll find plenty of ways to do so, some use Flash, others PHP and a database and there are even some Java applets out there (they still exist!). However when searching for a really simple chat I could integrate into the website of the 6th gamesday I found nothing that satisfied me: I didn't need logins or a user management nor multiple chat rooms, just a basic chat thats simple to handle and don't need maintenance.

Because of this I created my own chat with a few lines of PHP and JavaScript. The Simple Chat project is the refined and further simplified version of this chat:

That's pretty much it. 20 lines of PHP and about 40 lines of JavaScrip (with help from jQuery I admit). Not even a database and therefore nothing to keep an eye on. Just three simple files (if you include jQuery) and you can copy them around as you like. Take a look at the source code, it's really not much. If you need a small chat for your website feel free to use this code and extend or modify it as needed.

Performance wise I was a bit surprised. I wrote a small (and lousy) Ruby script to simulate multiple clients but even with 150 simulated clients the load on the webserver and I/O was still negligible (the test script eat almost all CPU time). The test script can't simulate more than 150 clients due to threading overhead (at least that's what I suspect) and one could write another more scalable test script but with 150 clients the chat can withstand much much more than it will ever get.

I've written an in depth explanation of the architecture and code but that's something for tomorrow. For now the idea and the code should speak for them selfs. If you have questions feel free to write a comment. :)

4 comments for this post

leave a new one

#1 by

Would you not be better off avoiding files, instead using a separate, long-lived process that you communicate with via a socket?

#2 by

That would avoid some I/O and the disc access. I played with the thought of using the "alternative PHP Cache" ( but it's an extension and therefore you have to install it. It would also make the polling requests a bit more complicated and costly (since PHP would be involved), disable the HTTP caching and may add maintenance overhead. Something like Redis (a key-value store, can also be used but it would bring the same problems (but features atomic list operations).

The core aspect of all that however is to avoid the I/O associated with files and another (maybe simpler) way to achieve that would be to simply use a RAM disk for the message buffer.

However the main aspect of that chat was the simplicity and the performance is just a "positive side effect". There are other ways to build a high performance chat (e.g. a small server which uses epoll() and a lock free list) but that would be a bit more complicated. The performance to code ratio is quite well as it is now. :)

#3 by

Great chat script, thank you. I've tried many chat scripts and yours does what i want. I wonder why you only have 2 comments, I thought there would be hundreds.

#4 by


I've tried to test your chat, but unfortunately, when I try to send something nothing happens. I'm just trying with your example.php with following permissions:

-rwxr-xr-x. 1 root root 6459 Sep 8 2010 example.php -rwxr-xr-x. 1 root root 72174 Sep 4 2010 jquery-1.4.2.min.js -rw-rw-rw-. 1 root root 0 Apr 22 23:37 messages.json

IS there something more I need to configure ?

Leave a new comment

Having thoughts on your mind about this stuff here? Want to tell me and the rest of the world your opinion? Write and post it right here. Be sure to check out the format help (focus the large text field) and give the preview button a try.


Format help

Please us the following stuff to spice up your comment.

An empty line starts a new paragraph. ---- print "---- lines start/end code" ---- * List items start with a * or -

Just to keep your skill sharp and my comments clean.