Reputation: 1452
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.
Upvotes: 4
Views: 10526
Reputation: 63139
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 :-)
Upvotes: 0
Reputation: 2796
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.
Upvotes: 1
Reputation: 17544
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
Upvotes: 1
Reputation: 1573
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.
Upvotes: 0
Reputation: 66851
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.
Upvotes: 7