1

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.

shasi kanth
  • 6,987
  • 24
  • 106
  • 158
  • 1
    Can you run PHP deamons on your host? – Jan Hančič Apr 06 '12 at 08:01
  • Yes, i hope the PHP Daemons can be created and run on my host. – shasi kanth Apr 06 '12 at 08:09
  • 1
    Well which way is it? You either can or you can't :) the reason I'm asking is because this is probably the solution that's available to you (writing a PHP websocket server) – Jan Hančič Apr 06 '12 at 08:11
  • Yes, i can create and run the php daemons on my host – shasi kanth Apr 06 '12 at 08:17
  • 1
    Here's some similar questions with very good tips/guidelines: [Scaling a chat app](http://stackoverflow.com/questions/5313641/scaling-a-chat-app-short-polling-vs-long-polling-ajax-php) - [Writing a chat application](http://stackoverflow.com/questions/3682198/writing-a-chat-application) - [Ajax Polling](http://stackoverflow.com/questions/35499/ajax-polling) - – Mike B Apr 06 '12 at 08:31

1 Answers1

2

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.

mega6382
  • 9,211
  • 17
  • 48
  • 69
Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
  • I would consider the second option, which you have pointed out. But what does it have to do with php daemons? – shasi kanth Apr 06 '12 at 08:32
  • 1
    A websocket server runs as a PHP deamon. – Jan Hančič Apr 06 '12 at 08:37
  • A small update: My host allows the creation of Daemons/daemonized processes. But the default timeout is 10 mins. It seems to create hurdles for me as the requests must be handled for more time. – shasi kanth Apr 09 '12 at 12:56
  • 1
    You can still write a deamon that will work around that issue, but I would suggest that you switch your hosting company. – Jan Hančič Apr 09 '12 at 13:17
  • Update: Finally i decided to implement my application using node.js and socket.io. I am planning to install them on a dedicated linux system with a public IP address. Then i can use this system to put my chat application and run it in an iframe in my project. – shasi kanth May 08 '12 at 05:49