CodeIgniter Forums

Full Version: Asyncronous work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm curious how people here deal with attempting to do things asyncronously.

For example, in my application I make several api calls to social networks. Sometimes an api call can take 1-2 seconds, but since php runs everything syncronously, a stack of 5+ api's can quickly delay a process to 10+ seconds.

I'm wondering how people get around that here. Messaging Queue? ReactPromise? NodeJS?

I do implement caching, which helps the 2nd time I make the call, but if a user hasn't logged in in a while and needs brand new versions of every feed, he will run into the delay.
Generally, if I am calling an API that may take some time to finish and needs to change based on the user, I prefer to use client-side JavaScript to call the API and/or update the page with the data returned from that API. If it's really going to take a while, you can always display cached data and/or a loading graphic for that portion of the page.
I've always used a library based on this article:
http://blog.markturansky.com/archives/205

It sends it using fsockopen and just doesn't wait for a response back.

We have to submit lead data to a crm and use this method after the user submits the form and it's successful. That takes up to about 4 seconds so we send it using the above method and it's instant and the user doesn't have to wait.
Message Queue every time.

The reason for this, it doesn't matter if its javascript or PHP. Both listen to MQ and both can send and receive. It's completely agnostic.

If I was having a page load depending on 3rd party data. I would load the page and in php fire off the "sends" to the queue which would in-turn ping all the social networks. It would then push back the result into another queue where javascript is listening and update the page.

When the page loads because its handed over to an MQ, there would be no delay. In-fact you can always have a static "Please wait" in those sections. They would be loaded in when javascript responds with the payload.

As you are doing now. You can extend the php to check against the timestamp say in redis/aeropsike and if past a certain time go to the social sites, otherwise retrieve cached info.

Pretty trivial to implement.
https://github.com/guzzle/guzzle
I mentioned recently this component, according to its documentation it supports asynchronous requests.
(05-31-2015, 02:34 PM)no1youknowz Wrote: [ -> ]Message Queue every time.

The reason for this, it doesn't matter if its javascript or PHP.  Both listen to MQ and both can send and receive.  It's completely agnostic.

If I was having a page load depending on 3rd party data.  I would load the page and in php fire off the "sends" to the queue which would in-turn ping all the social networks.  It would then push back the result into another queue where javascript is listening and update the page.

When the page loads because its handed over to an MQ, there would be no delay.  In-fact you can always have a static "Please wait" in those sections.  They would be loaded in when javascript responds with the payload.

As you are doing now.  You can extend the php to check against the timestamp say in redis/aeropsike and if past a certain time go to the social sites, otherwise retrieve cached info.

Pretty trivial to implement.

That's interesting, I had never thought of that approach. I use Amazons SQS for several tasks, but I had never thought of using it like this. What message queue do you use that interfaces with javascript directly?
(06-02-2015, 08:29 AM)albertleao Wrote: [ -> ]That's interesting, I had never thought of that approach. I use Amazons SQS for several tasks, but I had never thought of using it like this. What message queue do you use that interfaces with javascript directly?

ZeroMQ and here is the binding for it in nodejs.

Code:
https://github.com/JustinTulloss/zeromq.node