1

This is THE MOST noobish question ever, I see it done so often.

I want a PHP page which is constantly runnng in the background (the backend) to occasionally query the frontend with updated data. YES, this is the way I want to do it.

But the only way I know of querying a page is to re-create that php, with XHR - so, I would XHR "index.php?data=newdata", but this would create a new process server-side. What do I do?

(Please ask for more info or correct me if there is a better way of doing it)

drnessie
  • 877
  • 2
  • 12
  • 25
  • You could create an HTML5 `WebSocket` connection from server to client; that way client and server can send data to each other and receive immediately. However, `WebSocket` isn't supported by IE if I'm not mistaking and it's not commonly used either. – pimvdb Apr 09 '11 at 19:12
  • Why don't you have the HTML page periodically query another PHP script that checks the status of the first one? – Nathan Osman Apr 09 '11 at 19:13
  • One technology that can do what you need is [COMET](http://en.wikipedia.org/wiki/Comet_(programming)), which has PHP implementations. But is starting a server-side process really that much of a problem? Ajax is the most common way to go here. – Pekka Apr 09 '11 at 19:16
  • I know AJAX is the way to go, but I haven't stepped into that pool yet. @Pekka, starting a serverside process would mean that all the data previously collected by index.php would be lost from the second occurance. Ill check out this comet lib tomorrow. I am however, not intent in learning a whole new language (e.g Perl) to do this. – drnessie Apr 09 '11 at 19:21
  • Can you tell us more about what you're trying to accomplish? There might be easier, more straightforward ways to get there. The thing you're asking about isn't something that PHP does gracefully. – Charles Apr 09 '11 at 19:27
  • My intent is to create an online operating system but with a kernel backend, which will set it apart from other OSs – drnessie Apr 10 '11 at 05:00
  • OK. I've worked out what I need to do - my PHP backend is changing its function to basically redirect mouse and keyboard input, so I can just use basic AJAX. Of course, if anyone knows a way of creating PHP dynamically, that would also help. – drnessie Apr 10 '11 at 12:38

3 Answers3

2

This is a great SO question/answer to look at:

Using comet with PHP?

The upshot is, you can do it with PHP...

Community
  • 1
  • 1
AJ.
  • 27,586
  • 18
  • 84
  • 94
2

Another way to do this is to create a bridge from your Apache set up and Node, if you read through the guides about Node you will see that it is:

  • Designed for high loads of networking
  • Only spawns new threads when it need's to do blocking tasks such as I/O
  • Extremely simple to use, based on Google V8 (Javascript Engine)
  • Can handle thousands of concurrent connections

With the above in mind my road plan would be to create a database for your PHP Application, and create 2 connections to that,

  • Connection used in the PHP Application
  • Connection used within Node.

The Node side of things would be simple:

  • Create a simple socket server (20~ lines)
  • Create an array
  • Listen for new connections, place the resource into the array.
  • Attach an for the database
  • When the event get's fired, pipe the new data to all the clients in the array.

All the clients will receive the data at pretty much the same time, this should be stable, it's extremely light weight solution as 1K Connections would use 1 process with a few I/O Threads, the ram used would be about 8~MB

Your first step's would be to set up node.js on your server, if you google around you will be able to find how to do that, a simple way under ubuntu is to do:

apt-get install nodejs

you should read the following resources:

for more technical assistance you should connect to the #node.js irc server on freenode.net, those guys will really help you out over there! Hope this helps.

RobertPitt
  • 56,863
  • 21
  • 114
  • 161
1

COMET may be a way to go; however, having a finalized HTML page, and doing AJAX requests to get updates is the usual, and more robust way to do this. I would investigate ways to implement an Ajax based approached that is optimized for speed.

You say you are doing complex calculations, which you would have to repeat for each request when going the Ajax way. You may be able to help that e.g. by employing smart caching. If you write the results of whatever you do as JSON encoded data into a text file, you can fetch that with almost no overhead at all.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088