The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Generate graphs on server, display in browser

Hello, this is my first post here. I have quite a bit of Win32 and Javascript programming experience but am new to server-side scripting. Here is my problem:

I need to generate images on a server and send them to the user. In the HTML file, Javascript of the type

document.images[1].src=TmpImg

is waiting for the newly created image.

This image must be generated by a Windows application running either on the Windows 2003 server or on a PC to which the server has access. The app is heavy, so instead of launching it for every request, I plan to launch a tiny executable that sends a request string to the app.

I thought this was an extremely simple setup, but my searches tell me it's not. Exec and System seem not to be widely used, and the idea of having a GUI app running on a server seems not so popular (I can parse the request manually to prevent exploits).

Efficiency and speed are not important - there will not be many users. But I would be grateful for hints to a solution that is simple enough for a newbie in server-side scripting.

Thanks, but I have the app that generates the image, and do control it perfectly in a desktop environment. I want to know how I can do the same on a server... and how to send the generated image to the user's browser.

I did a little searching, and it seems GD does have a Windows version (Win32)

Thanks, really, I appreciate, but it's not what I am looking for. I am perfectly able to generate the images. It took me several years to develop my image package, and gd is definitely not able to generate the highly specific images I need. Generating images is not my problem - getting them back to the user's browser is what I do not understand.

If an image is available, the browser can download it. If you do something like "click here for image" the server script can generate the image but the browser will "hang" while it's waiting. Not good if it takes a while. To get it to populate a javascript array, or otherwise show via javascript code, You need a way for the browser to know when it's ready. PHP can't "push" to the browser, but the browser can "pull" from a PHP file. In other words, the server doesn't so much "inform" the page as it does "answer" it. I'm not sure if all browsers would act as expected if you used AJAX to send a request, and then wait for the response. If it worked, only the AJAX script would "hang", not the page in the browser. And you could trigger a "waiting" type of thing to let user's know what's going on. It might be better to have AJAX periodically check a file for a ready=true url=path response and then make the request for the image.

Ok, so you want to run a single instance of your image creation application and just let it run as a daemon, waiting for something to talk to it, so it can do something and then it can reply with the result to that client.

And if I understand correctly, you want to use php to execute a tiny executable process to interface between php and your daemon. And when the image is all done and created and the tiny executable gets the resulting filename, it will provide this filename to php and then the tiny executable process can just die off.

It sounds like exec() is what you want. Theres also proc_open() but it doesnt sound like you need to actually do any real back and forth communication, just send some data once, and wait for the reply, so exec would be simpler.

PHP Code:

// this script receives the http request initiated by javascript, and sends a response to it
$arg_string = prepare_the_args($_POST['foo'], $_POST['bar']);

// php will hang waiting for the executable to exit so it can return
exec('/foo/bar/tiny_executable.exe ' . $arg_string, $result);

// reply to javascript with the filename
echo $result[0];

I'm guessing your tiny executable would read stdin to get the arg_string, and write to stdout once it has aquired the filename. I say guessing because I haven't done any of this type of stuff yet

Ok, so you want to run a single instance of your image creation application and just let it run as a daemon

I'm guessing your tiny executable would read stdin to get the arg_string, and write to stdout once it has aquired the filename. I say guessing because I haven't done any of this type of stuff yet

If that sounds right, I can help with the javascript part too.

That sounds perfect, thanks a lot. TinyApp could receive the querystring via the commandline, too? How do I recognise in javascript that FatApp has finished? I also considered a "passive" solution, i.e. send the request, wait a second and try to load the image - no communication needed.

There is one point that worries me: Is it possible to run a Windows app, with real windows and message loops, on a server? I can move the window of FatApp outside the visible area, no problem, but I need an active window to do the drawing. If that is not possible, the only workaround would be to run the daemon on another desktop PC, and use net DDE to launch the request. Clumsy, and it needs extra hardware. Tomorrow I'll have access to the server, we'll see.
Thanks again for reading my post thoroughly, crmalibu

I'd recomend doing some validation before you put anything into the command you feed to exec(). see escapeshellarg() to escape, but consider some strong validation too. You need to program very defensively on the web.

Originally Posted by DosFan

How do I recognise in javascript that FatApp has finished?

This is where AJAX comes in, which is just an asynchronous functionality of javascript. When sending an ajax http request, the browser will send the request in the background asynchronously. The user can continue using the webpage, and will be unaware the browser is actually sending/loading something. Once the browser receives the response(from your php script), javascript will fire the onreadystatechange event. You can have javascript execute a callback function when this event gets fired. Your function would examine the response text your php script sent, and then make changes to the webpage based on it.

I'll use the jquery javascript library to simplify the ajax code needed.

There is one point that worries me: Is it possible to run a Windows app, with real windows and message loops, on a server? I can move the window of FatApp outside the visible area, no problem, but I need an active window to do the drawing. If that is not possible, the only workaround would be to run the daemon on another desktop PC, and use net DDE to launch the request. Clumsy, and it needs extra hardware. Tomorrow I'll have access to the server, we'll see.

I have no idea. My experience in programming for the desktop is limited to using the shell, and a few hello world programs in java.

*Consider some type of flood control for this whole thing. A user could send a ton of image creation requests to try to DOS you, and without some type of flood control or queue, you may end up with a ton of tinyapp processes running.

I am advancing. After a few hours of fumbling with the manual php install, I decided for the msi and got it running as fastcgi - just guessing that fastcgi was as good as normal or isapi, no hints provided by the installer.

So finally my first php script ran, and produced an "unable to fork" message for hello.exe...

Eventually, I managed to get hello.exe run, and found out that some things work, others not. MessageBox, for example, just produces a beep, and then the browser hangs (but can be stopped with Escape or killed with Task Manager).

It seems that all Windows functions that require a desktop don't work, but standardout echoes text directly into my browser. Greaaaat!

Just an update to a very old thread: I got it working. FatApp is running on a Windows server as a normal GUI app, and interprocess communication is done via files on disk. Not very elegant, but reliable...