How can I use PHP to delay server response for real-time reverse Ajax requests?

In summary: If the client doesn't send a request for more than x seconds then the server will give up and return a 404 page. If you want to send more than one message then you'd have to send a separate request for each message. In summary, using a reverse Ajax approach reduces traffic by holding the request until you have something to send back to the client.
  • #1
ngkamsengpeter
195
0
How can i use php to write a real-time chatting script like msn or yahoo messenger ? I know i can use php and mysql and check for new messagesin mysql table every seconds but this will cause high traffic . So is there any other way ?
 
Technology news on Phys.org
  • #2
You can use a reverse Ajax approach to reduce traffic. Or you can use any platform that supports sockets (i.e. flash or java).

With reverse Ajax, instead of querying the server every X seconds for new messages, you query the server once. The server holds the request (without timing out) until it has a message to send back to the client.
The client then receives the message and immediatly initiates another query to the server which holds the request again until it has something to return, thus repeating the process. It's important that the server be able to hold requests without wasting resources (such as hold the request with an active loop), otherwise you'll have high CPU usage at the server side.

With Flash you can use the XMLSocket object to connect to a port at the server. With sockets the server can send messages directly to the client, without the client having to monitor the server for new messages (i.e. once there are new messages they'll arrive at the client's end of the socket), so the process becomes more efficient.
You'd either use an existing server, such as the Flash Communication server (which i think now is called the Adobe Media server or something), or use PHP scripts as the server, or build your own server software which doesn't have to be a complex task.

Java would be similar to Flash.
 
Last edited:
  • #3
-Job- said:
You can use a reverse Ajax approach to reduce traffic. Or you can use any platform that supports sockets (i.e. flash or java).

With reverse Ajax, instead of querying the server every X seconds for new messages, you query the server once. The server holds the request (without timing out) until it has a message to send back to the client.
The client then receives the message and immediatly initiates another query to the server which holds the request again until it has something to return, thus repeating the process. It's important that the server be able to hold requests without wasting resources (such as hold the request with an active loop), otherwise you'll have high CPU usage at the server side.

With Flash you can use the XMLSocket object to connect to a port at the server. With sockets the server can send messages directly to the client, without the client having to monitor the server for new messages (i.e. once there are new messages they'll arrive at the client's end of the socket), so the process becomes more efficient.
You'd either use an existing server, such as the Flash Communication server (which i think now is called the Adobe Media server or something), or use PHP scripts as the server, or build your own server software which doesn't have to be a complex task.

Java would be similar to Flash.

Can you give me more details about the reverse ajax ?About the flash , can i use the normal server or i need to use the Flash Communication server .PHP also support socket right?
 
  • #4
You don't need to use the Flash Communication Server with flash. You can build a server with Java, .NET or PHP. If you use PHP you will need to run the PHP app independently from the web server (i.e. not on top of an IIS or Apache web server), using the PHP CLI for example, as described here:
http://devzone.zend.com/node/view/id/1086

Once you have a server running, you create a flash movie that instantiates an XMLSocket and connects to the PHP server, and exchanges any necessary information back and forth.

I actually recommend the Reverse Ajax approach because it's a fairly new approach, more compatible (no flash/java plugins) and more accessible and easier to use.

You'll still use PHP and MySQL for storing and accessing user messages. The difference is in how you use Ajax. Commonly you'd use Ajax to query a PHP script (which checks for new messages on the database) every x seconds, which generates a lot of traffic.
What you need to do is find a way to hold a request at the server until you have something to return.

Then for example at client side you use Ajax to query the PHP script (same as before). The PHP script checks for new messages. If there are any to return then it returns them, otherwise it waits (sleeps or blocks with a loop) checking for new messages every so often (so now the PHP script checks every x seconds, rather than Ajax). Once there are new messages to return then the PHP script stops sleeping or blocking and finally returns a response containing the messages.

At the server side there's usually a timeout limit. For example if a request takes more than S seconds to process or more than a given number of resources (CPU time, Memory, etc) then the server (IIS, Apache...) terminates the process and returns a time out back to the client.
You can either avoid timeouts or just deal with them.
 
Last edited by a moderator:
  • #5
-Job- said:
You don't need to use the Flash Communication Server with flash. You can build a server with Java, .NET or PHP. If you use PHP you will need to run the PHP app independently from the web server (i.e. not on top of an IIS or Apache web server), using the PHP CLI for example, as described here:
http://devzone.zend.com/node/view/id/1086

Once you have a server running, you create a flash movie that instantiates an XMLSocket and connects to the PHP server, and exchanges any necessary information back and forth.

I actually recommend the Reverse Ajax approach because it's a fairly new approach, more compatible (no flash/java plugins) and more accessible and easier to use.

You'll still use PHP and MySQL for storing and accessing user messages. The difference is in how you use Ajax. Commonly you'd use Ajax to query a PHP script (which checks for new messages on the database) every x seconds, which generates a lot of traffic.
What you need to do is find a way to hold a request at the server until you have something to return.

Then for example at client side you use Ajax to query the PHP script (same as before). The PHP script checks for new messages. If there are any to return then it returns them, otherwise it waits (sleeps or blocks with a loop) checking for new messages every so often (so now the PHP script checks every x seconds, rather than Ajax). Once there are new messages to return then the PHP script stops sleeping or blocking and finally returns a response containing the messages.

At the server side there's usually a timeout limit. For example if a request takes more than S seconds to process or more than a given number of resources (CPU time, Memory, etc) then the server (IIS, Apache...) terminates the process and returns a time out back to the client.
You can either avoid timeouts or just deal with them.

I don't know how to use reverse ajax and also how to avoid timeout ,please give me some example.
 
Last edited by a moderator:
  • #6
Well, I'm actually involved in a project that will use reverse Ajax, but it's in ASP.NET. Maybe you can take something from this. If you've ever used threads then you have come across the Sleep() function. Basically when you make an HTTP request to a web application running on a web server (IIS, Apache...) the web server's worker process (w3wp.exe in IIS) responsible for the application either starts a new thread which will service the request or queues the request if all allocated threads are already being used.

This is true for PHP web applications as well. When you make a request to a PHP web application, the web server either starts a new thread to service the request, or queues it until a thread is available (the maximum number of threads is customizable).

Our goal is to block the server's response to the Ajax request until the server has something to return. When the Ajax request reaches the server it will eventually be serviced by a thread. If you want to delay the processing of a thread by 5 seconds you can use Sleep(5000) in ASP.NET/Java or Sleep(5) in PHP. Therefore the following code (ASP.NET) will delay the server's response until it has something to return:
Code:
while(!HasSomethingToReturn()){
    Sleep(5000);
}
//we get here if there's something to return so write that something to the client

You should be aware however of some implications of using this strategy. Because most servers have a specifiable maximum number of threads, each reverse Ajax request that blocks for say 40 seconds is completely blocking a whole thread. If you have a max of 25 threads, then 25 reverse Ajax requests would completely slow down your server.
So you either need to ensure that you have sufficiently high max number of threads such that you can be confident that you server will always offer good performance, or just not use Web Server worker threads for processing these types of requests, which i recommend (though it involves a little more work).
In ASP.NET this is made easier with Asynchronous handlers, which will run on a separate thread that you create and hence doesn't take over one of our limited web server threads.
Learn more about it here: http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/
An alternative to ASP.NET would be doing a server in Java or .NET or PHP. PHP probably has something equivalent to Asynchrnous handlers but it's not something i have looked into.

Anyway, if you're not doing a web app that will be used by hundreds of users concurrently then you can use PHP with Sleep (making sure to specify a large thread maximum).
 
Last edited:

FAQ: How can I use PHP to delay server response for real-time reverse Ajax requests?

What is PHP programming?

PHP (Hypertext Preprocessor) is a server-side scripting language used for web development. It is used to create dynamic web pages and can be embedded into HTML code.

What are the basic concepts of PHP programming?

The basic concepts of PHP programming include variables, data types, functions, control structures, and arrays. These concepts are used to write code and manipulate data in PHP.

How do I solve common PHP programming problems?

To solve common PHP programming problems, it is important to understand the basics of the language and use proper syntax. You can also refer to online resources, forums, and documentation for troubleshooting help.

What are some useful tools for PHP programming?

Some useful tools for PHP programming include IDEs (Integrated Development Environments) such as PHPStorm and Visual Studio Code, debugging tools like Xdebug, and frameworks like Laravel and CodeIgniter.

How can I improve my PHP programming skills?

To improve your PHP programming skills, you can practice coding regularly, read code written by others, and participate in online communities and forums. You can also take online courses or attend workshops to learn new techniques and best practices.

Back
Top