tags:

views:

3167

answers:

6

I have made a chat script using php, mysql and jquery. It uses json to get data from the server. It makes fixed interval requests to the server with the lastly fetched message id to get new messages from the server. But when multiple users will be chatting then thousands and crores of requests will be made to the server within an hour and the hosting people will block it for sure.

Th gmail chat uses socket I think. Because it does not sends fixed interval requests for sure. Could any one of you please give me some sample code or some direction to solve this issue.

Please I need help desperately.

Many thanks in advance. My respect and regards for all.

+7  A: 

If the host you are using would "block it for sure" if it's making that many requests, then you may want to consider getting a different host or upgrading your hosting package before worrying about your code. Check out how Facebook implements their chat:

The method we chose to get text from one user to another involves loading an iframe on each Facebook page, and having that iframe's Javascript make an HTTP GET request over a persistent connection that doesn't return until the server has data for the client. The request gets reestablished if it's interrupted or times out. This isn't by any means a new technique: it's a variation of Comet, specifically XHR long polling, and/or BOSH.

ryeguy
Many thanks for your kind help. Really appreciate it.
Kunal
Even I am using iframe to make cross domain ajax requests. My chat script will be installed on other websites but the messages and users list will be on my server. Need to learn more about persistent connection though, would appreciate some hints if you get some free time. Thanks a yahoo!
Kunal
A: 

Why would the host block that? Your making a standard http request for a page, if your host doesn't allow that then it's time to switch.

As for using sockets, there is no native ability to connect to a socket via javascript, although I believe JSocket is a lib that allows you to bridge a socket through an embedded flash which is actually connected to your server. Haven't looked for a jquery plugin that does this, might be one.

Your server side code would also change drastically (persistent vs polling is very different) so you'd have your work cut out for you.

I recommend just doing what you are doing and upgrade your host if it can't handle it. Unless your going to have a huge number of users on at a time? A caching system so your not hitting the db on every single request can probably speed things up if it gets that busy.

savageguy
Ho w can I add caching from the page from which I am sedning json data to the client? PLease advice.
Kunal
Sure thing. Let's say you average 300 requests for an update a second, this would typically translate into 300 db queries per second. You instead could have a simple file which holds the latest json data, on each request check the file last mod, if its older then a second, hit your db and...(limit)
savageguy
update the file with the latest data, otherwise hit the file with your json data. This would mean 1 query per second regardless of the number of requests for update. You'd need to do some testing to see when this becomes worth it response wise but it will certainly be faster then the alternative.
savageguy
The filw will reside on the server of course. Then if I have to update the file from database then again I will have to run a cron job to update the file. In this case the overload is getting higher I think. Cause 1 process to update the file and again other http requests to read the file.
Kunal
no cron necessary - in the file you hit for the data check if it's out of date or not, if it isn't just use the file otherwise update and use the file. If nobody uses the chat for a few minutes here and there the updates will stop and you don't waste resources.
savageguy
This way I am simply reducing requests to the database server but not the web server. Even to check the file I have to make periodical requests to the web server. Would appreciate your view on this.
Kunal
You can output a static file faster then a dynamically generated one from a database. Your not only reducing the number of trips to the database but also the amount of work your server needs to do.
savageguy
i will explain in details to you: my chat script will be installed on different websites using a simple js file only, the js file will insert html on the pages. it basically inserts one iframe with an absolutely placed div on the bottom right corner of the page. contnd...
Kunal
now the iframe sends requests to a ph page which checks if the user is authenticated or not. if yes then only it loads the chat page which in turn requests the messages from another page which returns only json data. contnd...
Kunal
after studying all the replies and about using persistent connection ai am really confused about how to implement the persistent conn and how can it help me? shall i implement it on the page which returns the json data? contnd...
Kunal
i was observibf gmail chat with firbug. it does not send periodical reqs for the messages, but for diff events like mouse move, blur, focus etc...also messages were shoiwng up automatically without any reqs...that got me vry surprised...i donno how they are doing it!
Kunal
if you do the same you might get some interesting idea...let me know your views on this. also about using a static file, how do you want to generate the file and how would you check if its out of date or not?
Kunal
+1  A: 

You may find it useful to see an example of 'comet' technology in action using Prototype's comet daemon and a jetty webserver. The example code for within the jetty download has an example application for chat.

I recently installed jetty myself so you might find a log of my installation commands useful:

Getting started trying to run a comet service

Download Maven from http://maven.apache.org/

Install Maven using http://maven.apache.org/download.html#Installation I did the following commands Extracted to /home/sdwyer/apache-maven-2.0.9

> sdwyer@pluto:~/apache-maven-2.0.9$ export M2_HOME=/home/sdwyer/apache-maven-2.0.9
> sdwyer@pluto:~/apache-maven-2.0.9$ export M2=$M2_HOME/bin
> sdwyer@pluto:~/apache-maven-2.0.9$ export PATH=$M2:$PATH.
> sdwyer@pluto:~/apache-maven-2.0.9$ mvn --version
-bash: /home/sdwyer/apache-maven-2.0.9/bin/mvn: Permission denied

> sdwyer@pluto:~/apache-maven-2.0.9$ cd bin
> sdwyer@pluto:~/apache-maven-2.0.9/bin$ ls
m2 m2.bat m2.conf mvn mvn.bat mvnDebug mvnDebug.bat

> sdwyer@pluto:~/apache-maven-2.0.9/bin$ chmod +x mvn

> sdwyer@pluto:~/apache-maven-2.0.9/bin$ mvn –version

Maven version: 2.0.9
Java version: 1.5.0_08
OS name: “linux” version: “2.6.18-4-686″ arch: “i386″ Family: “unix”
sdwyer@pluto:~/apache-maven-2.0.9/bin$

Download the jetty server from http://www.mortbay.org/jetty/ Extract to /home/sdwyer/jetty-6.1.3

> sdwyer@pluto:~$ cd jetty-6.1.3//examples/cometd-demo

> mvn jetty:run

A whole stack of downloads run

Once it’s completed open a browser and point it to: http://localhost:8080 and test the demos.

The code for the example demos can be found in the directory:

jetty-6.1.3/examples/cometd-demo/src/main/webapp/examples
Steerpike
+1  A: 

Right or wrong, a hosting company might get cranky for a couple reasons:

1) Odds are good they are using apache prefork. Each chat request is probably gonna be a new connection and thus hog up a single apache process. Each apache process eats anywhere from 1mb of memory to 100mb of memory.

2) If they maintain the database server and you, the client, suck at database programming, you can hammer their database. "Suck" means anything from "no proper indexing" to "makes a bazillion tiny queries instead of nice fat ones".

As has been suggested above, make sure your code uses persistent connections. Also:

1) Implement a back-off algorithm on the client. Poll the server once a second during activity, then back off to five seconds, then ten, twenty, etc... That way you dont hammer the server when there is no activity.

2) Multiple tabs will kill you. User opens 10 tabs and they all have your chat widget polling the server once a second? Bad news. Even if your host doesn't get pissed, your performance will degrade.

If this thing gets huge, design your system in a way that you can run the chat-server bits independently from the rest of your web application. In otherwords, the clients would be making a request to "chat.yourwebapp.com", which in turn is running on something like lighttpd.

Cory R. King
Many thanks for your kind reply. Would appreciate if you could pls explain point number 1. What are the benefits fo running lighttpd? Pls explain.
Kunal
Different servers are good idea for getting messages and users list. But how would I maintain the session among diff servers? I think web services can help, what do u think?
Kunal
If your session_id is stored on the datatabase and your cookie is for the top of the domain (.yourdomain.com instead of www.yourdomain.com), all the servers can share the same session.Take a look how most session handlers do business--sharing the session across servers isn't too hard :-)
Cory R. King
lighttpd is basically a "slender" web server that doesn't use much memory. You can run your php chat bits using FastCGI. I wouldn't worry to much about it yet though--just design your app so that you can "fork" your chat stuff to run independently of the rest of your app.
Cory R. King
Many thanks for your help. Really appreciate your time. :)
Kunal
A: 

try socket in javascript

http://code.google.com/p/jsocket/

hackerdz
A: 

You think about embedding a small Flash movie in the page and then use sockets to handle the communication with server. This will take a lot of the load from the server and would make much more easier to keep everything in sync. The UI could still make with JavaScript.
It you will stay with your JavaScript solution then silently ignore my answer :-)

Hippo
Hey that's sounds really cool. Would love to get my hands on on something like this. But the problem is I have no idea how I can control a flash movie with JS functions and how it can communicate with the mysql db and php code on server :(Appreciate your help. Thanks.
Kunal