Hassan Dad Khan
Hassan Dad Khan

Reputation: 645

Single PHP thread to respond to two asynchronous ajax requests

I am implementing a synchronization process between two devices. Web and mobile. I have planned an algorithm but not sure about the possibility of its implementation. My algorithm is,

  1. Mobile will generate a ajax request to PHP script to sync its data onto database.
  2. PHP scripts starts a transaction, then insert data into to database that mobile sent and collects data that it has to send to mobile and send resposce but do not commit.
  3. Mobile receives response, starts a transaction, and send another ajax request as acknowledgement to php script and commit at mobile side.
  4. PHP script receives acknowledgement ajax request and commit its data as well that i developed in setp 2.

My question is, is it possible to use single php thread to work with two synchronous ajax request?

Upvotes: 0

Views: 138

Answers (2)

Hassan Dad Khan
Hassan Dad Khan

Reputation: 645

I finally ended up using websockets. And to implemet websockets in PHP i used Ratchet. I was able to get what i was trying to implement.

Upvotes: 0

Sanjeevi.V
Sanjeevi.V

Reputation: 643

I will leave the php concepts to experts.

We had a similar scenario, where we would send the data to server and insert it with a flag value (e.g., incomplete), then we will acknowledge back to device saying that xyz rows were inserted. It will also contain an unique identifier per device so that we can keep track of which device sent what data, we will also maintain the details of unique identifier per device, unique transaction id (uuid) and timestamp in a separate table (transaction_status). The acknowledgement will be updated in clients, this will basically remove the transactions from the sync queue maintained on the client.

When the next set of records from the same client reaches server, the server will query for the matching entries in the transaction_status table. if the entries are already available, it means that the acknowledgement did not reach the client and server will respond with necessary data in such case.

If the next set of records did not contain the entries which were sent during previous sync then those considered as successful sync and the incomplete flag will be update.

I understand that this might be vague, but i will try to create a diagram in my free time to clearly depict this scenario.

Please leave a comment if you need more clarification.

Anyone having a better approach, please feel free to share.

Upvotes: 2

Related Questions