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. :)

8 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 ?

#5 by

The Chat works fine. I wonder if there is a way to increase the number of posts before they vanish

#6 by

Hi Abie,

In the PHP code there is a variable named $messages_buffer_size (default 10). This is the number of messages that are kept in the JSON file. If you increase it to e.g. 50 new visitors to the chat will see the most recent 50 messages.

This page contains the entire source code of the chat (below the chat box): It's easy to spot $messages_buffer_size there.

Note that the chat discards old messages by design. Thanks to this you can just leave the chat script on a server and it won't overflow with random garbage messages from bots.

If you want to preserve the chat messages you can enable logging. It's one commented out file_put_contents() call that is marked with "Optional". Again, see the source code. But the log is not shown in the chat itself, it's just a text file on the webserver.

In case you want to know the details of how the chat works you can read this post: It covers the the whys and hows of the chat.

Happy programming Stephan

#7 by

Hi Stephan,

first of all thanks for your job and sharing it. I have two questions about simple chat: 1) long messages couldn't be sent, you have to split them. Is there a length limit? 2) is it possible to put a message like "write your name" if $_POST['name'] was not changed? Thanks is advance Franz

#8 by

Hi Franz,

sorry for the late reply. I had some quite busy days setting up my own business as a freelance programmer.

About 1): The chat itself doesn't care about the message length. I did a quick test with messages up to 256 KByte and had no trouble. But the environment where you run the chat might impose limits, e.g. in the webserver configuration. Also maybe some overzealous security system inspected the message content and filtered something it wasn't supposed to filter. I haven't encountered these things but from reading the docs it's possible. If you want you can send me a mail with some details about the messages you were trying to send. Then we might be able to debug it further.

About 2): The chat is very simple and by default it can't do that. But adding it doesn't require big changes. The easiest way I can thing of is by changing line 157 of the example (

<input type="text" name="name" id="name" value="Anonymous" />


<input type="text" name="name" id="name" value="" required />

With that change the default name is empty (value="") but the user is required to enter something before sending a message (required). The browser itself will show a corresponding error message in the users language.

Hope that helps Stephan

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.