2

Here is my backend layout: a request is put through a socket.io socket that communicates to php_script. PHP returns to socket.io that dispatches yet another request to php_script2 and answers the frontend regarding the original request (to php_script). php_script2 sleeps in its algorithm and occasionally sends data back to the socket layer. The problem appears when the same user that issued the original request tries to interact with the socket while php_script2 is running. (The frontend freezes upon trying to send new info to socket.io). This is:

1 - user sends input

2 - socket.io received input

3 - socket.io sends input to php as an http_request with the following options:

var options = {
    host : 'localhost',
    path : path,
    method : method_type,
    rejectUnauthorized: false,
    requestCert: true, 
    agent: false,
    headers : headers
};

4 - PHP (php_script) processes request and sends feedback to socket layer

5 - Socket layer receives feedback in the functionToPhp, interacts with the user socket socket.emit("success") and issues a new php request to php_script2 with the same options as before

6 - Frontend confirms reception of the data packet (php_script2 is running) and if the user tries to send new information to the socket that requires new interaction with php, the frontend element freezes (in this case a textarea, it is made up to freeze but unfreeze as soon as "success" is received from the socket.io). It unfreezes only as soon as the php_script2 ends all its process including sleep while it should unfreeze right away.

I did it this way (nodejs->php->nodejs=>php->nodejs) precisely because I didn't want the original request from the user to nodejs to be "stuck", so, right in the second layer of nodejs (before the =>) I output "success" to the user and issue a new request from nodejs to php as to imitate threading and go async. Unfortunately this isn't happening and the user is still waiting for the callback after php_script2 has ended. Why exactly is this working? I understand how a server works but I'm not really understanding how can the request remain tied if it is issued again in a localhost perspective. I hope I was clear, tyvm for your help...

Fane
  • 1,978
  • 8
  • 30
  • 58
  • 3
    Some code would be good. – Gennadiy Litvinyuk Jan 06 '16 at 07:43
  • we need code, and schema of your implementation is much better than a lot of text explaining your current architecture... also i'm confused where socket is really implemented bu which technology : NodeJS ? PHP Socket ? 3rd party JavaScript Socket ? – Halayem Anis Jan 06 '16 at 08:33

1 Answers1

1

While php_script2 is sleeping it will not respond to any new requests from the same client-side session, because it is asleep. PHP is not going create a new 'instance' or process of php_script2.

Try pinging your PHP script regularly for the data from the client side rather than having a PHP script sleep(). There is some helpful information in this question: How can I stop PHP sleep() affecting my whole PHP code?

Sean Fahey
  • 1,850
  • 3
  • 26
  • 36
  • I ended up using session_write_close() , worked just fine, thank you so much for your help! – Fane Jan 08 '16 at 18:47