Web application interacts bi-directional with server program? - php

I want to write a web application to play chess against the engine Crafty. I'm not new to PHP and javascript, but must learn how to interact with a server process : how can a web application and/or (jQuery) ajax interact bi-directionally with a (linux) program running on the server?
At this moment i am developing on (Apache) local host. Crafty is installed on my Ubuntu PC. This well-known chess engine has no GUI, it runs in terminal by the command
$ /usr/games/crafty
and so you can play chess against it and even see it's calculations :
I can make Crafty run by PHP, using the functions proc_open() or exec(), and most documentation i found states that the output stream should be a file .. But i think i don't want such setup, because then the webpage should be constanty polling that file (eg. by ajax) to see if some new data was appended, right?
How can Crafty talk to the web page directly, saying "i have calculated another variation" or "i have decided a move" etc, then display this info on the web page and let the user give some counter move, just like in terminal. Isn't it possible to use some session / stream / listener?
I have no clue at all, can anybody point me in a right direction?

I recommend you make use of fifos and the & operator - here is why:
You do not want to start crafty on every PHP request, you want to start it only once per game
You don't want to have crafty end at the end of your Request
Your move-requests will want to interact with this allready running instance.
So what I would do is something like:
Prepare a pair of FIFOs using mkfifo - you can do this from PHP or from the shell
On game start, run something like /usr/games/crafty <stdin.fifo >stdout.fifo 2>stderr.fifo &
For your moves, make an AJAX PHP request write to stdin.fifo
For the server moves do long polling with AJAX, on the server side opening stdin.fifo, then stream_select()

Related

PHP: "Callback server" for a Windows server?

I am programming PHP applications where I need to move processing routines from the client (browser) to the server. We have our own dedicated Windows servers.
Let us say that a shopper buys something and the system has to generate a nice and complex invoice PDF (and do a lot other things that takes some seconds) and after this send it to the client as fast as possible. Right now, I have running these time-consuming routines in a hidden Iframe and hoping that client is not breaking the routine by going to another page. It is not a good solution.
A much better solution would be to trigger some kind of software on the Windows server that does the processing instead of the browser (and does it instantly).
I could use "Scheduled Tasks" in Windows but the quickest it can run is each minute. I need something that can run instantly. Do you know what can do this on a Windows server? Like some kind callback server (software).
To make sure user navigation doesn't stop your scripting you can use ignore_user_abort(true);
You can also use exec create background process and run your php script
Example
exec("start /B php Notify.php");
To learn more about execfunction click here
You can find more information by googling stuffs.

How to integrate C++ application into PHP website?

I have a c++ application that processes a sentence and responds back to the respective user. I have a website using php that I would like to post data to this application and receive this response. Ideally, the C++ would run on the webserver and not have to be fully loaded each time it is used, but i haven't been able to get a simple 'hello world' (with cgi or c++) to work on the webserver.
What would be the easiest way to integrate a c++ application into a website? Should this work with any webhost?
To Integrate them you can take two approaches.
1) Integrate C++ as a function into PHP with http://www.php-cpp.com
You can create a function, say my_complex_function, that when called in PHP, will execute a C++ code. You can read the site's documentation for information about it.
2) Keep them separate and communicate through HTTP/Pipes/Sockets/Other
You could build a C++ daemon that opens up some kind of communication interface like a Socket, then with PHP you open a socket to it, send the information through the socket, and receive the answer there too.
You can find socket examples for PHP here: http://php.net/manual/en/sockets.examples.php

Feed data to remote process

I am not really sure how to phrase my question, but I'll try to be as clear as possible.
I want to create a demo website which allows people to input sentences which are then sent to a remote host, and classified in a Python script on that host. Basically, the Python script, when initialised, needs to load some big classifier files into NLTK classifier objects, which is preferably done only once to save time. So I would like to keep these objects alive. I realise I can execute a while loop in order to wait for any incoming data. The loop would parse the data and do whatever with it:
#pseudocode for Python script
while True:
if(some_remote_input){
parse, classify, etc.;
}
My question: What is a recommended package to collect data sent remotely? socket, perhaps?
And how would I go about this in PHP/AJAX? The desired procedure is like this:
User enters a sentence in the web app
Sentence is validated/prepared
Sentence is sent to remote host as input for python script (which is always running (like a server)
The result of the python classification script is returned to the website
Results are formatted and printed to the user
I am concerned about step 3.
I prefer not to integrate the Python environment on the server where the website is hosted, as there is not a lot of free space and it requires a lot of annoying packages.
If you need any more info, please let me know!
What you are looking for is a message broker or message queue system.
There are several message brokers available:
RabbitMQ
Kafka
AMQ
This lets you pipe data betwen the message transmitters (web server) and message consumers (the natural language processing pipeline) in a way that is agnostic to what technologies are used in the web server and the NLP-pipeline

How do i check for a change in a file that has been included in an HTML doc through an AJAX script?

I am writing a JavaScript for an in-browser IM client for the sake of practicing and learning JavaScript and AJAX.
I need to be able to check for a change in the file size of a text file that is being used as a temporary storage for 40-80 SQL entries that contain messages so that it can update the display.
At the moment I am using a setInterval function to periodically check for a change in file size using short PHP script, but this can cause issues, if the interval is to long, messages are delayed, if it is shorter, it means a lot of php scripts running very quickly, which takes up server resources.
What is the best way to do this if the main concern is to reduce server resource usage?
(I am running my server off of a rather low tech PC I've scraped together(2gb ram, 2.8ghz AMD seperon processor))
Preferably, I would want to do this using an AJAX event triggered by someone sending a message, I.E. When user B triggers the event that edits the file by pressing enter, that triggers a function on user A's side that updates the HTML file
Any ideas? I am open to any solution to this particular problem. I gave specific examples of what I want to happen in the specific languages in order to give a better idea of what it is I am attempting to do.
If there is a way to do this that isn't JavaScript/PHP, I'd also be open to exploring that as an option.
Doing this with PHP can be a bit cumbersome. You could try doing something like long polling where you keep the HTTP request open until the server has new data to send to the user. If messages are sent frequently, this might not be ideal. You might want to consider using event-driven web technologies like node.js with something like Socket.IO.
In any case, you'll likely want to maintain a connection with the server if you want to get the message in near real-time. There are ways to use WebSockets with PHP as well, but PHP isn't really the best for this because it's not designed to keep scripts running for long periods (also see What exactly entails setting up a PHP Websocket Server?).
Browsers & HTTP/ AJAX generally work by a "pull" model. The browser/ or AJAX sends the server a request, then the server answers a response.
There isn't generally much provision for the server to contact the browser, to "push" an event. This can however be simulated by a long-running request, to which the server writes data when the event/ or events occur.
For example, this could be a request that answers "empty" after a timeout of 10-30 seconds.. or the server returns & answers immediately, if there are event(s) in its queue.
With a Java server this is easy to do, and I've used this successfully for event notification in a major integration project a few years back.
However I'm not sure in PHP how much ability there is (probably very near zero) to maintain an overall server state, coordinate or communicate between threads/requests, or maintain event queues.
You could look into something like a Java webapp running on Tomcat. All you need is a basic web.xml and one Servlet class, and you can build just about anything from there.

PHP CLI + Ajax for web terminal

I'm trying to determine the best approach to providing an Ajax based terminal using PHP. I haven't made an attempt at writing it yet but having rolled the idea around, the only way I could see it possible, would be 2 scripts:
Script 1; handles Ajax communication
between server and client browser. when a
request is made to use the terminal,
it connects to (or starts as a service then
connects to) Script 2 via a socket.
Script 2; performs the system calls,
passing back output to the Ajax
script for output via the socket.
There are multiple holes I can see in this though, and I'm wondering if anyone has created/seen a set of scripts that can perform these tasks? Any insight would be greatly appreciated!
Thanks :)
Edit: I think I was unclear about a few things. I've found a few scripts that imitate terminals, providing nearly the functionality that I'm looking for, such as AjaxPHPTerm (http://sourceforge.net/projects/ajaxphpterm/)
The problem is that, I'm trying to find a method that permits interaction with shell scripts. If a script prompts Press any key to continue, or Select option [x], using AjaxPHPTerm, it just hangs or drops out of the shell script.
That's why I started thinking sockets, or streams; some way of forming a direct I/O stream to the system calls.
Http is stateless and AJAX, sockets or any other technology based on pages generated by server will not change it magically. Whatever tricks You would use, it will be not efficient and simply not worth the effort (In my opinion at least).
The problem seems to be that AjaxPHPTerm is actually closer to a shell than a terminal (glancing at the code, it seems to do its own CWD handling, and has a simple read-eval-print loop).
Assuming a Posix-compatible OS on the server, the proper way to implement this would probably be to use the pseudo-terminal facility, so that your web terminal appears like a virtual terminal on the system, that running programs can interactively access.

Categories