How to create a streaming API with NodeJS How to create a streaming API with NodeJS javascript javascript

How to create a streaming API with NodeJS


You would want to set up a system that keeps track of incoming requests and stores their response objects. Then when it's time to stream a new event from FriendFeed, iterate through their response objects and responses[i].write('something') out to them.

Check out LearnBoost's Socket.IO-Node, you may even just be able to use that project as your framework and not have to code it yourself.

From the Socket.IO-Node example app (for chat):

io.listen(server, {    onClientConnect: function(client){        client.send(json({ buffer: buffer }));        client.broadcast(json({ announcement: client.sessionId + ' connected' }));    },    onClientDisconnect: function(client){        client.broadcast(json({ announcement: client.sessionId + ' disconnected' }));    },    onClientMessage: function(message, client){        var msg = { message: [client.sessionId, message] };        buffer.push(msg);        if (buffer.length > 15) buffer.shift();        client.broadcast(json(msg));    }});