Handle Multiple request from clients to a PHP page with MySQL - php

i have a Ubuntu server and a mobile app, i make request with JSON from the mobile app to a php page on the server that handle the request connecting to a MySql DB, but i notice that sometime happen that when the request are a lot in the same time, some request drop, i want know if there is a way to handle multiple request, without lose some.

When do the requests drop? Whilst sending a SQL query to the Mysql database?
The cause of this can be either the server itself, which just can't handle that many requests at a time (probably just configured to do so), the script you're using (badly written) or the database.
After you find the cause, start looking for the cure.

Related

How do i check for a change in a file that has been included in an HTML doc through an AJAX script?

I am writing a JavaScript for an in-browser IM client for the sake of practicing and learning JavaScript and AJAX.
I need to be able to check for a change in the file size of a text file that is being used as a temporary storage for 40-80 SQL entries that contain messages so that it can update the display.
At the moment I am using a setInterval function to periodically check for a change in file size using short PHP script, but this can cause issues, if the interval is to long, messages are delayed, if it is shorter, it means a lot of php scripts running very quickly, which takes up server resources.
What is the best way to do this if the main concern is to reduce server resource usage?
(I am running my server off of a rather low tech PC I've scraped together(2gb ram, 2.8ghz AMD seperon processor))
Preferably, I would want to do this using an AJAX event triggered by someone sending a message, I.E. When user B triggers the event that edits the file by pressing enter, that triggers a function on user A's side that updates the HTML file
Any ideas? I am open to any solution to this particular problem. I gave specific examples of what I want to happen in the specific languages in order to give a better idea of what it is I am attempting to do.
If there is a way to do this that isn't JavaScript/PHP, I'd also be open to exploring that as an option.
Doing this with PHP can be a bit cumbersome. You could try doing something like long polling where you keep the HTTP request open until the server has new data to send to the user. If messages are sent frequently, this might not be ideal. You might want to consider using event-driven web technologies like node.js with something like Socket.IO.
In any case, you'll likely want to maintain a connection with the server if you want to get the message in near real-time. There are ways to use WebSockets with PHP as well, but PHP isn't really the best for this because it's not designed to keep scripts running for long periods (also see What exactly entails setting up a PHP Websocket Server?).
Browsers & HTTP/ AJAX generally work by a "pull" model. The browser/ or AJAX sends the server a request, then the server answers a response.
There isn't generally much provision for the server to contact the browser, to "push" an event. This can however be simulated by a long-running request, to which the server writes data when the event/ or events occur.
For example, this could be a request that answers "empty" after a timeout of 10-30 seconds.. or the server returns & answers immediately, if there are event(s) in its queue.
With a Java server this is easy to do, and I've used this successfully for event notification in a major integration project a few years back.
However I'm not sure in PHP how much ability there is (probably very near zero) to maintain an overall server state, coordinate or communicate between threads/requests, or maintain event queues.
You could look into something like a Java webapp running on Tomcat. All you need is a basic web.xml and one Servlet class, and you can build just about anything from there.

Simple data exchange between servers

I am completely new to web development, so please forgive me in case this question is superfluous.
Here is what I am trying to do:
I have multiple Arduinos (equipped w/ EthernetShields) that are collecting and displaying data (using sensors and LCD screens). All Arduinos communicate with a webserver hosting a MySQL database and a webpage visualizing the data. The Arduinos are themselves capable of hosting minimalistic webservers that can run some simple html/php/etc...
My question is:
What is the most straightforward way to implement the communication between the Arduinos and the main server, so that I can send data back and forth between them, without any user interaction?
The data consists of relatively few integer values and short strings of text. Security is not an issue.
Edit for clarity:
How to continuously send data back and forth between two web-servers?
Assuming that I use PHP, what is a simple way to do this? All tutorials for the GET and POST methods included the use of 'form action', which to my understanding requires user interaction.
If I understand you right, you have a central web server that needs to communicate with a whole bunch of arduinos. You need to send information in both directions between the arduinos and the central server. And, you want to use web (http / tcp / ip ) protocol to do this.
You face a choice:
Do you want to have the central server initiate the communication? Or do you want to have each arduino initiate the communication?
I think the second choice is pretty good. It means that you can add new arduinos to your system without somehow reconfiguring the central server. But, I don't know much about your application so there might be some reason this is a bad idea.
So, what you do is implement a simple web CLIENT (not a browser) on each arduino. Then, on a regular schedule you have the each arduino do a web request to the main server. Depending on the amount of data you need to send from the arduino to the server, you can use a GET or a POST. If the data is small, you can use a GET request. For example if it's temp and humidity you can send the request like this once a second.
http://server/upload.php?temp=65&humidity=78
In response to this, the central server can handle this data correctly. You'll be able to tell which arduino it's from by the client internet address REMOTE_ADDR.
Then, the server sends a response that contains whatever data the server needs to send to the arduino.
See how this goes? each second each arduino hits the web server saying "here's what I have for you. What have you got for me?"

Run a AJAX query which uses PHP code that limits queries/sec on client side?

I have a PHP Code, that uses a web service to query some data. However, this web service limits queries per second based on the server (i m not sure about the exact mechanism, but it seems to be the IP address)
I m using ajax to query this data from a PHP file on my server, but is there some way I can let my client search this data for themselves by faking that they are requesting the data rather than my servers?
The issue is I am getting a RateExceeded error message from server, as soon as two clients are requesting the same page, which is rather obvious since the webservice is only seeing one server, which is my server.
So, can I somehow, make it happen that when these clients query the data, the service is rather thinking that the request is being originated via these clients, rather than my server.
You can't really fake it, but if you could query the webservice directly via AJAX, it should be seeing the clients' IP addresses. Note that if you're doing any kind of processing on the data that the WS returns, you'd have to perform it in JavaScript, on the client side.
If the request from your server to the WS contains any confidential data (e.g. some kind of access key) which the clients must not see, this approach is useless.

File resource persistence in PHP

I'm developing a simple chat web application based on the MSN protocol. The server communicates with the MSN server through a file resource returned from fsockopen (). The client accesses the server via XMLHttpRequest. The server initially logs in, and prints out the contact list (formatted in an HTML table) which the client receives through the responseText () of the XMLHttpRequest object.
Here's the problem. The file resource that is responsible for communication with the MSN server must be kept alive in order for all chat related functions to work (creating conversations, keeping track of offline/online state changes, etc). However in order for the XMLHttpRequest to complete, the PHP script must finish execution. Which means the client will get no response from the XMLHttpRequest while the chat session is in progress.
Whats worse is a file resource cannot be serialized, meaning I cannot simply store the chat session in a $_SESSION [] placeholder.
So, my question is, is there any possible way for me to 'transfer' a file resource from one file to another?
In most languages its not possible to pass file handles between applications - AFAIK most operating systems don't allow it either.
The solution is to keep the server process running as daemon - which means it needs to run outside of the webserver.
See
http://symcbean.blogspot.com/2010/02/php-and-long-running-processes.html
and
http://www.phpclasses.org/browse/package/5758.html
C.
A possible solution would be to have a PHP script on the server-side that just doesn't end ; this way, the resource corresponding to the fsockopen call would never be deleted, and the connection wouldn't be closed.
About this, you might want to search for the term "comet" ; the basic idea is to have a script that runs forever on the server-side, that sends updates to the client whenever it's necessary.
Instead of having the browser send an Ajax request every X seconds, you'd keep an openened connection between the client and the server -- just note that, unfortunatly, PHP is often said not to be the best tool for that job...
On stackoverflow : [php] comet
The resource can't survive the end of the request unless you create PHP extension that does it (like persistent MySQL connections do with mysql_pconnect() for example). However, you could use Comet technology and for example Bayeux protocol supported by Dojo toolkit among others, to talk to the server. That would require either standalone server or long-running request, in latter case ensure that PHP and webserver time limits would not kill that request for running too long.
Thanks everyone for the suggestions. Before I started this project I had considered using comet technology, but decided against (PHP/Apache don't seem to implement well). I've come up with a hacked together solution, not the most elegant but workable.
One PHP script is responsible for the MSN server communication, it will run as long as the user is active. It writes data to a file (email_out), as well as reads data from a file (email_in). Whenever the client sends a AJAX request a separate PHP script will write any POST data to the file (email_in) and will return any data from (email_out). Both scripts will not read/write data until they finally have access to the file (as there will be fighting for the file resource).
I don't know, suggestions? This is certainty not the most efficient means of doing things but it's really the only PHP/apache solution I could think of.

PHP Jabber: if I login and check messages and disconnect, on the other users end I will show up as disconnected

Am not sure if what I am doing is absolutely correct. But here goes:
User logins into chat via web-based interface
User is informed of updates via Comet
User enters details which goto a PHP file which further connects to a Jabber server
Now the problem is that when the user wants to send a message, it's simple, run php in which i connect to jabber server and send the message. The problem arises when I am waiting for a message. Cause if I login and check messages and disconnect, on the other users end I will show up as disconnected.
Am I approaching this problem in a wrong way? Should I directly connect to the Jabber server (via javascript) instead of a PHP layer in between? How to recieve messages via PHP?
I haven't tried it out, but you might want to look at xmpphp. Secondly, you might want to consider keeping the user logged in to the XMPP server (aka a Jabber server) for as long as they're logged in to your website. You probably want to have a timeout of some kind in case they leave your website and don't come back.
As for whether or not you should connect via JavaScript, I don't see why you couldn't. I would suggest that you go for whatever seems the simplest to you. You might want to check out Strophe, which I hear good things about, for that case.
The only XMPP library that I've used extensively though is headstock, but that requires using python and Kamaelia.
this is an inherent problem (or feature) with http - there are no lasting connections (not really). you need a workaround, there is no real solution.
you could do it with java or flash, but that's not really nice (javascript 4tw!).
the other possibility would be to create an intermediate client what translates connections between the browser and the webserver to connections between the webserver and the jabber server. messy, but possible.
or maybe there is an API that helps with this.
directly connecting to the jabber server via javascript
i possibly slept through the latest ajax-inventions, but afaik you can only communicate with the host the source-html file comes from (ignoring greasmonkey and addons). no different domains, no different ports, period. unless you're going to teach your jabber server how to serve your chatpage-html to the browser, this will get problematic. moreover, staying connected doesn't even work, because that would require multipart-responses. those are only supported by mozilla, and this is why the ugly duckling COMET even exists in the first place. comet itself is a workaround to avoid the inability to hold connections while transfering data.
So the issue, as far as I can tell, is that when the Jabber user on the other end responds. The problem there, at least in part, is that the user is responding to another user on the Jabber server, yet you want the php script to be aware that this response has taken place without holding the connection open (which makes sense since the script is no longer running, probably).
One option, albeit a really silly one, is:
Have a php script that can broker a connection to the Jabber server for both sending and receiving for the user on your page,
Use AJAX to send messages for the user (the AJAX would point to the above script, the script would send the message.)
Have a Javascript infinite loop that pings the same script ever 10 seconds or so, checking in to see if there are messages. If there are, they get passed back to the client and output to the user.
There are only two issues with the above:
1) If the user isn't connected when the message is transmitted, will the php script still see/get the message?
2) A client side loop that makes ajax requests every 3 seconds would probably be a huge drain.
Solution 2:
OpenFire jabber server. It comes with a web chat client built in, and it has an addon called Fastpath, which is meant to handle HTML-based chats on the client end (like the "chat with an agent now!" feature on too many support pages.)
We use this at work and it is very customizable, can be integrated with other scripts (for instance, if you want a script that fills in the user details from their login, or adds some custom avatar, or whatever), and it (OpenFire) has tons of other extensions and addons that, if this isn't what you want, they probably have what you are looking for.

Categories