How to begin developing chat api, like the one stackoverflow uses? If it is open source, where can i find it, if not can anyone guide me how to build a similar chat api?
5 Answers
Now its the time of comet.
comet is reverse ajax.If you are using ajax in chat applications u need to check everytime for database updations but in the case of comet its all about server side events.
We can set the certain events @server side then it will automatically update the webpage when the database is getting updated.that is we do not need to give requests all the time.
So that we can avoid the server headache due to large number of requests and the application will be very much faster.
This is a live chat example using comet.
check it out: http://www.zeitoun.net/articles/comet_and_php/start
its beyond ajax
-
I was wondering over the last few days if something like this is possible? I thought I will get answers like it is against the client-server model where the client sends a request to the server. Nice to know about comet. Will try it. Thanks sirin – Sandeepan Nath Dec 05 '10 at 09:29
-
@Sandeepan Nath :Welcome.Dnt forget to promote me.ok? – sirin k Dec 05 '10 at 09:35
-
@sirin, this uses prototype as the library, is this possible using jquery? – mrN Dec 05 '10 at 10:15
-
@mrNepal:Ya ofcourse.In that example prototype is used to implement ajax engine at the client side.So you just replace it with jquery.i hope its very easy to implement ajax engine using jquery as well as prototype. – sirin k Dec 05 '10 at 10:19
-
You know of any demo i can check regarding comet and jquery – mrN Dec 05 '10 at 10:33
-
I dnt knw abt that.coz im not working with jquery. – sirin k Dec 05 '10 at 10:35
-
+1 sirin for sharing this valuable info. I will try it out soon – Sandeepan Nath Dec 06 '10 at 06:51
-
keep in touch sirinibin2006@gmail.com thats my id – sirin k Dec 06 '10 at 13:47
-
thanks for sharing! very good explanation of comet which I was lookin for. But I have two questions: 1) Wouldn't I get problems caused by "max_execution_time" defined in php.ini? (default is 30 sec) 2) Is it OK for server when there will be around 100 opened and running php-scripts? (100 persons online in chatroom) – Roman Oct 30 '11 at 19:33
-
so I just tried this: php while(1){ flush(); echo $c++; sleep(1);} ?> and after 30 seconds I've got an error "Max execution time..." – Roman Oct 30 '11 at 19:54
-
try this example:http://www.zeitoun.net/articles/comet_and_php/start – sirin k Nov 16 '11 at 18:23
You can build a very simple PHP chat room with jQuery's AJAX functionality if you don't want to bother with the complexity of COMET. Regardless of what the server side API looks like, you can probably interact with it using jQuery from the client.
Clients can poll the server using jQuery code like this:
$(document).everyTime(pillowchat.settings.message_poll_frequency, function() {
if(pillowchat.state.poll == true){
getMessages();
}
});
jQuery POST requests could be sent like this:
$.post("chat.php", {
"attribute":"important string"
},
function(data){
response = JSON.parse(data);
processNewMessages(response);
});
They could be requests for new messages, active users, or contain new messages from the client.
The API on the server can be implemented a million different ways. I wrote a simple chat using PHP and CouchDB that worked pretty well. More detail and source code is available here: http://trillworks.com/nick/2011/08/13/pillowchat-how-not-to-build-a-chat-room-with-jquery-phpillow-and-couchdb/
I wouldn't recommend this approach if your expect more than 30 people in the room. When stress testing this design, I found apache couldn't handle all the traffic. Make sure you include some sort of flood detection.

- 191
- 5
This chat plugin looks like the facebook one: http://anantgarg.com/2009/05/13/gmail-facebook-style-jquery-chat/ and this is a tutorial http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=335
I hope it helps.

- 2,359
- 18
- 24
-
-
Open this example in Firefox and then you console firebug to see the ajax requests. http://anantgarg.com/chat/samplea.php – Ives.me Dec 05 '10 at 08:05
-
Ok, but I am looking for something like SO's chat, your reference is really helpful, but I also require group chatting – mrN Dec 05 '10 at 08:22
I would highly highly recommend checking out the APE project. It stands for Ajax Push Engine and it uses Comet Server techniques/technology. This project is designed to handle tens of thousands if not hundreds of thousands of users at a time and it provides the server end and the JS interface client. It is compatible with all the major JS libraries.
Its well thought out, clean, and most importantly FREE!
Also I am sure that there are CMS plugins that exist that utilize it. The DrupalChat module has been talking about using it.

- 3,302
- 4
- 28
- 47