tags:

views:

122

answers:

5

Hi,

I have a page that I am performing an AJAX request on. The purpose of the page is to return the headers of an e-mail, which I have working fine. The problem is that this is called for each e-mail in a mailbox. Which means it will be called once per mail in the box. The reason this is a problem is because the execution time of the imap_open function is about a second, so each time it is called, this is executed. Is there a way to make an AJAX call which will return the information as it is available and keep executing to prevent multiple calls to a function with a slow execution time?

Cheers, Gazler.

+1  A: 

It sounds as though you need to process as many e-mails as have been received with each call. At that point, you can return data for all of them together and parse it out on the client side. However, that process cannot go on forever, and the server cannot initiate the return of additional data after the http request has been responded to, so you will have to make subsequent calls to process more e-mails later.

Eric Mickelsen
Thanks for your response, the issue with this however is that all the e-mails would have to be retrieved before a single one could be read.
Gazler
In that case, you have a dilemma. You could process a fixed number per call, or request one initially and then the rest, or any division you can think of that will produce the most desirable result for your application. You could even make two simultaneous asynchronous calls, one requesting the first and the other requesting all.
Eric Mickelsen
Or you can use one request for every message.
Douwe Maan
A: 

I think you need to clarify the question a little.

+2  A: 

There are technologies out there that allow you to configure your server and Javascript to allow for essentially "reverse AJAX" (look on Google/Wikipedia for "comet" or "reverse AJAX"). However, it's not incredibly simple and for what you're doing, it's probably not worth all of the work that goes into setting that up.

It sounds like you have a very common problem which is essentially you're firing off a number of AJAX requests and they each need to do a bit of work that realistically only one of them needs to do once and then you'd be good.

I don't work in PHP, but if it's possible to persist the return value of imap_open or whatever it's side effects are across requests, then you should try to do that and then just reuse that saved resource.

Some pseudocode:

if (!persisted_resource) {
    persisted_resource = imap_open()
}

persisted_resource.use()....

where persisted_resource should be some variable stored in session scope, application scope or whatever PHP has available that's longer lived than a request.

Then you can either have each request check this variable so only one request will have to call imap_open or you could initialize it while you're loading the page. Hopefully that's helpful.

Bialecki
Thanks for the reply, I have tried storing the result of imap_open() in a session, but I am not sure you can store a resource like that in a session.
Gazler
PHP has a solution to persistent connections for databases, but I'm not sure if there's one for an imap connections. Each request is handled in a sandboxed environment, so the connection would be closed once the script execution finishes.
Anurag
+1  A: 

The server-side PHP script can be configured to send the output as soon as its generated. You basically need to disable all functionality that can cause buffering, such as output_buffering, output_handler, HTTP compression, intermediate proxies...

The difficult part is that you'd need that your JavaScript library is able to handle partial input. That is to say: you need to have access to downloaded data as soon as it's received. I believe it's technically possible but some popular libraries like jQuery only allow to read data when the transfer is complete.

Álvaro G. Vicario
+1  A: 

Batch your results. Between loading all emails vs loading a single email at a time, you could batch the email headers and send it back. Tweak this number till you find a good fit between responsiveness and content.

The PHP script would receive a range request in this case such as

emailHeaders.php?start=25&end=50

Javascript will maintain state and request data in chunks until all data is loaded. Or you could do some fancy stuff such as create client-side policies on when to request data and what data to request.

The browser is another bottleneck as most browsers only allow 2 outgoing connections at any given time.

Anurag