How to implement chat using jQuery, PHP and MySQL?

I made a chat script using php, mysql and jquery. It uses json to get data from the server. It makes requests at a fixed interval to the server with the last message identifier retrieved to receive new messages from the server. But when several users will chat, thousands and hundreds of requests will be sent to the server within an hour, and people hosting will probably block it.

Th gmail chat uses a socket, I think. Because it does not send requests at a fixed interval for sure. Can any of you please give me a code example or some direction to solve this problem.

I need help desperately.

Thank you very much in advance. I respect and respect everyone.

+4
source share
6 answers

If the host you are using will "probably block it", if it makes so many requests, then you may want to get another host or upgrade your hosting package before worrying about your code. See how Facebook implements its chat:

The method that we have chosen to receive text from one user to another involves loading an iframe on each Facebook page and given that the iframe Javascript has an HTTP GET request over a persistent connection that does not return until the server has data for the client. The request will be restored if it is interrupted or timeout. This is not in any way a new method: it is a variation of the comet, in particular, XHR prolonged interrogation and / or BOSH.

+7
source

You may find it useful to see an example of comet technology in action using the comet daemon prototype and http://maven.apache.org/

Install Maven using http://maven.apache.org/download.html#Installation I ran 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 supply 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 

The entire download package starts

After its completion, open a browser and point to it: 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 
+1
source

Right or wrong, a hosting company may face several reasons:

1) The odds are good, they use preprock. Each chat request is likely to be a new connection and thus take off one Apache process. Each apache process is powered from 1 mb of memory to 100 mb of memory.

2) If they support the database server, and you, the client, suck when programming the database, you can clog their database. "Suck" means anything from "without proper indexing" to "makes a few simple queries, not nice thick ones."

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

1) Implement a debugging algorithm on the client. Polling the server once per second during activity, then shutting down for up to five seconds, then ten, twenty, etc. Thus, you do not clog the server when there is no activity.

2) Several tabs will kill you. The user opens 10 tabs, and all of them have a chat widget, polling the server once a second? Bad news. Even if your host does not get angry, your performance will degrade.

If this thing gets huge, design your system so that you can run the chat server bit regardless of the rest of your web application. In other words, customers will make a request to "chat.yourwebapp.com", which, in turn, works on something like lighttpd.

+1
source

try socket in javascript

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

+1
source

Why is the host block? You make a standard HTTP request for the page, if your host does not allow this, then you need to switch.

Regarding the use of sockets, there is no way to connect to the socket via javascript, although I believe that JSocket is a lib that allows you to connect the socket through the built-in flash memory that is actually connected to your server. Not looking for a jquery plugin that does this, maybe one.

Your server-side code will also change a lot (constant and polling is very different), so you can turn off your work for you.

I recommend just doing what you do and updating your host if it cannot handle it. If you do not have a huge number of users at the same time? The caching system, so that you do not get into db for each individual request, may speed up work if it is busy.

0
source

You are thinking of embedding a small Flash movie on a page, and then use sockets to handle communication with the server. This will require a large load from the server and simplify synchronization. The user interface can still work with JavaScript.
You will be left with your JavaScript solution, and then silently ignore my answer :-)

0
source

All Articles