How to implement chat using jQuery, PHP, and MySQL? - php

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.

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.

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

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.

try socket in javascript
http://code.google.com/p/jsocket/

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.

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 :-)

Related

How to do load testing of a PHP website from both ends

We have developed a website in PHP for small services. Before production, we have to do performance and load testing from server side (that is Apache) and from client side.
From Client side, I want to know what is the average response time overall and for each object etc. For back-end side (apache web-server), I want to know how many request (clients) it can handle before its performance start to degrade.
Is there any open source tool as we are on Linux platform, for this purpose. Or is there any website(s) available (freely) that can do all of these testing.
We are more concerned about load testing where we want to request to our website from 1000 users at same time (for example) and want want to check client and server side different performance metrics.
Since you are running Apache, you could use ab to stress test the server side of your application.
Apache HTTP server benchmarking tool
ab is a tool for benchmarking your Apache Hypertext Transfer Protocol (HTTP) server. It is designed to give you an impression of how your current Apache installation performs. This especially shows you how many requests per second your Apache installation is capable of serving.
Let's look an example:
ab -k -c 100 -n 10000 example.com/
By issuing the command above, you will be hitting http://example.com/ with 100 simultaneous connections until 10 thousand requests are met.
Here you have another tutorial how to use ab
Stress-Test Your PHP App With ApacheBench
Some other helpful tools are:
Apache JMeter
httperf
If you want a more in-detail view of your php objects and all that stuff I would suggest you to check blackfire. As I know they offer a free version for local testing and only one application. But I don't think that you need this one for now.
For your front end I would suggest you a set of tools that help you to see how your page performs in different devices:
Lighthouse
PageSpeed Insights
Pingdom
You can find the list of free and open source load testing tools at http://www.opensourcetesting.org/category/testing-tools-overview/performance/?menu-page=overview, currently there are 59 (and counting).
The narrowed down list of the most advanced cross-platform open source load testing tools can be found in Open Source Load Testing Tools: Which One Should You Use? article, it also has feature comparison matrix, sample scripts and reports.
Given your application is in PHP my expectation is that you don't have a lot of expertise in other programming languages so the most obvious choices would be in:
Tsung where you can create your load test using XML files
Apache JMeter which also stores test scripts in XML format, however you can use GUI for test logic creation which might be faster and easier.
Both tools provide record-and-replay functionality so you will be able to build your test scenario skeleton using browser.

Best strategy for sending commands to Arduino: Send UDP packet OR get configuration from a dynamic web page?

I would like to send some configuration parameters to an Arduino Ethernet board, and I came up with two potential solutions. I would be grateful if you could give your thoughts on using either one of those.
The first (obvious) option is to send a UDP command containing the configuration to the Arduino. This, however, requires a reconfiguration for most routers to enable port-forwarding or disabling the firewall.
So my second option is to get the configuration by accessing a web page from Ardino which will contain all the required information (this will be updated using a PHP script on the server). This page will be checked every minute or so and update the configuration if a new one is found. The requirement for communication every minute is not a problem since there is also traffic on the other direction (my Arduino logs temperatures in a web site).
Which method is the best and are there any pitfalls I should avoid?
I recently finished my WebPlayer.ino where it got pushed commands over the web as to what files to play from the SD card to the MP3 chip, along with volume and speed control. Similar to your goals, I had to pull a bunch of pieces from all over into it. So it may be a nice example of how to parse the web commands.
In either UDP or TCP and using the native Ethernet library, one needs to read the character string from the ethernet.client. Regardless of direction and TCP vs UDP. The one advantage I have seen with UDP is that there are some examples using the "String" type to get the data as a whole, but that is insignificant. I would stick with TCP, look at my example as how to read the web servers requests.
Additionally, the Webduino library is a wrapper that simplifies getting the commands. It provides JSON and the like. So that would be very useful for what your doing. But it uses a huge chunk of RAM and ROM. Hence my project did not use it.

Using WebSocket on Apache server

With all the buzz around WebSockets, it's pretty hard to find a good walkthrough on how to use them with an Apache server on Google.
We're developing a plugin, in PHP (symfony2), which will run from time to time kind of a chat instance. And we find WebSockets more interesting, standard and quick than AJAX for this matter. The thing is, we don't have much sysadmin ressources in our group and we find hard to gather good informations on the following matters:
Can we run a WebSocket instance on a traditional Apache, dedicated server, and if yes, do you have useful links for us?
If we need to mod the server, what kind of tools would you recommend knowing that we are not too skilled in sysadmin so we can't afford to have a high maintenance b*** on this.
Thank you very much,
ps: we'll link back to your blog/site as we'll make a technical/informational post on our devblog about this part of our app.
Thank you again!
As #zaf states you are more likely to find a standalone PHP solution - not something that runs within Apache. That said there is a apache WebSocket module.
However, the fundamental problem is that Apache wasn't built with maintaining many persistent connections in mind. It, along with PHP, is built on the idea that requests are made and responses are quickly sent back. This means that resources can very quickly be used up if you are holding requests open and you're going to need to look into horizontal scaling pretty quickly.
Personally I think you have two options:
Use an alternative realtime web technology solution and communicate between your web application and realtime web infrastructure using queues or short-lived requests (web services).
Off load the handling of persistent connections and scaling of the realtime web infrastructure to a realtime web hosted service. I work for Pusher and we fall into this category.
For both self-hosted and hosted options you can check out my realtime web tech guide.
One path is to use an independent installed web sockets server.
For PHP you can try:
http://code.google.com/p/phpwebsocket/ or http://github.com/Devristo/phpws/
There are some other projects which you can try as well.
Basically, you need to upload, unpack and start running the process.
On the frontend, you'll have javascript connecting to the server on the specific port.
Most websocket servers have a demo which echoes back whatever it hears, so this is a good place to write some test code. You may even find a rudimentary chat implementation.
The tricky part is to monitor the web socket server and to make sure it runs smoothly and continuously.
Try to test on as many browsers/devices as possible as this will decide on which websocket server implementation you choose. There are old and new protocols you have to watch out for.
I introduced another websocket server: PHP Ratchet (Github).
This is better and complete list of client & server side codes and browser support.
Please check this link.
Another Path is to use a dedicated websocket server.
Try Achex Websocket Server at www.achex.ca and checkout the tutorials.
OR
If you really want Apache, check out Apache Camel. (but you have to set it up and its a bit more complicated than achex server)
http://camel.apache.org/websocket.html

Apache/PHP test server throttle by request

I've recently set up a test server running in a virtual machine on my computer so I can do such things as interactive debugging with XDebug. For the most part it's pretty sweet, but I've run into a snag when running multiple requests to the server at once from the same client.
The problem is that guest-host network connection doesn't really exist as a physical connection, so it will run as fast as the computer hardware will allow. This isn't usually a big issue, but I'm trying to implement APC file upload monitoring, and this requires an AJAX request to run in parallel to the file upload to monitor its performance. In the real world, the network would introduce lag and latency and suchlike, leaving enough unused bandwidth for the AjAX request to run in parallel with the file upload. However, in the test machine, the AJAX request can't fetch any data from the server until the upload is finished as there's absolutely no bandwidth left available to it.
Is it possible to set up some kind of bandwidth management in the virtual machine (in Apache, PHP or some Linux utility) that could limit the bandwidth available per HTTP request? For example so that each request is limited to 1mbps, but several requests can exist between the client and the server at the same time? I'm hoping that if this can be done it will allow the AJAX request to fetch its data while the upload is progressing instead of being stalled until the upload actually completes.
I tried a utility called IPRelay, but I don't seem able to get it to work, or at least not in a way that limits per request.
What you're asking for is called Traffic Shaping.
Lighttpd (an alternative to Apache) supports this natively
For Apache, there are a few ways of doing it.
mod_bandwidth - A 3pd module (that hasn't been updated recently) which appears to do the same thing.
mod_bwshare - 3pd module designed to combat DOS attacks, but may be helpful.
Here's a ServerFault Question that may be relevant...
Thanks for the reply. However, I found a handy little utility for Linux called iprelay that lets me throttle connections, it seems to let me have multiple connections open with each connection throttled to the specified limit. That's what I've been using today for testing my APC code and it all seems to be working fine.

Communication between PHP and application

I'm playing with an embedded Linux device and looking for a way to get my application code to communicate with a web interface. I need to show some status information from the application on the devices web interface and also would like to have a way to inform the application of any user actions like uploaded files etc. PHP-seems to be a good way to make the interface, but the communication part is harder. I have found the following options, but not sure which would be the easiest and most convenient to use.
Sockets. Have to enable sockets for the PHP first to try this. Don't know if enabling will take much more space.
Database. Seems like an overkill solution.
Shared file. Seems like a lot of work.
Named pipes. Tried this with some success, but not sure if there will be problems with for example on simultaneous page loads. Maybe sockets are easier?
What would be the best way to go? Is there something I'm totally missing? How is this done in those numerous commercial Linux based network switches?
I recently did something very similar using sockets, and it worked really well. I had a Java application that communicates with the device, which listened on a server socket, and the PHP application was the client.
So in your case, the PHP client would initialize the connection, and then the server can reply with the status of the device.
There's plenty of tutorials on how to do client/server socket communication with most languages, so it shouldn't take too long to figure out.
What kind of device is it?
If you work with something like a shared file, how will the device be updated?
How will named pipes run into concurrency problems that sockets will avoid?
In terms of communication from the device to PHP, a file seems perfect. PHP can use something basic like file_get_contents(), the device can just write to the file. If you're worried about the moment in time the file is updated to a quick length check.
In terms of PHP informing the device of what to do, I'm also leaning towards files. Have the device watch a directory, and have the script create a file there with something like file_put_contents($path . uniqid(), $command); That way should two scripts run at the exact sime time, you simply have two files for the device to work with.
Embedded linux boxes for routing with web interface don't use PHP. They use CGI and have shell scripts deliver the web page.
For getting information from the application to the web interface, the Shared file option seems most reasonable to me. The application can just write information into the file which is read by PHP.
The other way round it looks not so good at first. PHP supports locking of files, but it most probably doesn't work on a system level. Perhaps one solution is that in fact every PHP script which has information for the application creates it own file (with a unique id filename, e.g. based on timestamp + random value). The application could watch a designated directory for these files to pop-up. After processing them, it could just delete them. For that, the application only needs write permission on the directory (so file ownership is not an issue).
If possible, use shell scripts.
I did something similar, i wrote a video surveillance application. The video part is handled by motion (a great FOSS package). The application is a turn-key solution on standardized hardware, used to monitor slot-machine casinos. It serves as a kiosk system locally and is accessible via internet. I wrote all UI code in PHP, the local display is a tightly locked down KDE desktop with a full screen browser defaulting to localhost. I used shell scripts to interact with motion and the OS.
On a second thought:
If you can use self-compiled applications on the device: Write a simple program that returns the value you want and use PHP's exec() or passthru() or system().

Categories