Search code examples
phpjavascriptajaxproc-open

JavaScript back and forth with running program


Problem:

I'm trying to see if I can have a back and forth between a program running on the server-side and JavaScript running on the client-side. All the outputs from the program are sent to JavaScript to be displayed to the user, and all the inputs from the user are sent from JavaScript to the program.

Having JavaScript receive the output and send the input is easily done with AJAX. The problem is that I do not know how to access an already running program on the server.

Attempt:

I tried to use PHP, but ran into some hurdles I couldn't leap over. Now, I can execute a program with PHP without any issue using proc_open. I can hook into the stdin and stdout streams, and I can get output from the program and send it input as well. But I can do this only once.

If the same PHP script is executed(?) again, I end up running the program again. So all I ever get out of multiple executions is whatever the program writes to stdout first, multiple times.

Right now, I use proc_open in the script which is supposed to only take care of input and output because I do not know how to access the stdout and stdin streams of an already running program. The way I see it, I need to maintain the state of my program in execution over multiple executions of the same PHP script; maintain the resource returned by proc_open and the pipes hooked into the stdin and stdout streams.

$_SESSION does NOT work. I cannot use it to maintain resources.

Is there a way to have such a back and forth with a program? Any help is really appreciated.


Solution

  • This sounds like a job for websockets

    Try something like http://socketo.me/ or http://code.google.com/p/phpwebsocket/

    I've always used Node for this type of thing, but from the above two links and a few others, it looks like there's options for PHP as well.