开发者

How to make a php backend communicate with an HTML frontend?

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开发者_JS百科 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)


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

Using comet with PHP?

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


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:

  • http://nodejs.org/
  • http://www.youtube.com/watch?v=jo_B4LTHi3I
  • http://jeffkreeftmeijer.com/2010/experimenting-with-node-js/
  • http://www.slideshare.net/kompozer/nodejs-and-websockets-intro
  • http://remysharp.com/2010/02/14/slicehost-nodejs-websockets/

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜