tags:

views:

202

answers:

1

Hey everyone,

I am working on using node.js's connection abilities to continiously long poll a php script and I was wondering if anyone knows the thoery behind (maybe even a code sample) linking to php in node.js? I was thinking I need a new client and I add a request for the php page and then I add a response event listener which then fires off a event function which grabs the returned data and throws it back into the server function.

I am, however, a noob and need some initial guidance since their api documentation is not the easiest to read; the English is too wordy and it's white font on a dark background...not nice.

Thanks,

A: 
var sys = require('sys'),
   http = require('http'),
   url = require("url"),
   path = require("path"),
   events = require("events");

var twitter_client = http.createClient(80, "192.168.23.128");

var tweet_emitter = new events.EventEmitter();

function get_tweets() {
var request = twitter_client.request("GET", "/?url=ajax/session", {"host": "192.168.23.128"});

request.addListener("response", function(response) {
    var body = "";
    response.addListener("data", function(data) {
        body += data;
    });

    response.addListener("end", function() {
        sys.puts(body);
        var tweets = JSON.parse(body);
        if(tweets.length > 0) {
            tweet_emitter.emit("tweets", tweets);
        }
    });
});

request.end();
}

setInterval(get_tweets, 5000);


http.createServer(function (req, res) {
sys.puts("accessed Server");

res.writeHead(200, {'Content-Type': 'text/plain', "Access-Control-Allow-Origin": "*"});

var t = JSON.stringify({id:"test"});

var listener = tweet_emitter.addListener("tweets", function(tweets) {
    res.write(tweets);
});

 res.write(t);
  res.end();
}).listen(8124);
sys.puts('Server running at http://127.0.0.1:8124/'); 

This seemed to work. Taken from a mixture of other tutorials

sammaye