I have a website that is implemented in php/mysql, and is hosted at hostmonster (shared hosting).
I am planning to incorporate a chat feature in my website, that enables users to chat with each other. There are approx. 1000 users on my site.
Currently, i have implemented an ajax strategy that sends requests to the server on a periodical basis (say, every 4 seconds) to get the json response about online users and messages.
This is the sample code that sends requests:
var timer, delay = 4000;
timer = setInterval(function(){
$.ajax({
type: 'POST',
url: 'update.php?user_id=2',
success: function(html){
$('.chat_messages').append(html);
}
});
}, delay);
But as i see, more the number of users, more the requests for every 4 seconds, and more burden on Apache server and database to deliver the response.
So i am planning for an alternate solution, that has no overhead on the server.
I have heard about few servers like APE, Nginx, Node.js, StreamHub that use COMET/reverse ajax technology to serve the requests, and are efficient for my purpose.
But the problem is that, i dont have permission to install a third party server on my hostmonster cpanel.
Is there any other way around, like HTML5 web sockets?
EDIT: By the way, i am also interested to rebuild my application on Java, if there is a viable solution.
As you figured out requesting what’s new ever 4 seconds for each user will not scale. And it won’t even work for a chat application as delays between messages would be too long, rendering your chat unusable.
If I were you I would first look into drop-in chat solutions that don’t require any (or very little) programming on your part. You usually just insert some JavaScript code into your page, and the third party that provides the chat does all the rest (serving files, running the chat server, etc …). I haven’t used any of the avaliable solutions so I can’t recommend any, but a quick google search yielded this: https://www.meebo.com/websites/ this should give you a general idea of what I’m talking about.
If the above will not suite you then you have a quite a large project ahead of you. What you will have to do is write a websocket server (and then your chat application on top of that). If I wanted to explain how to do it to you I could write a book about it, so I’ll just say this: google is your friend. You are not the first who needs this. So search stackoverflow for tips and there are also open source PHP websocket servers out there (this being one).
Good luck.