javascript - Building simple nodejs API with streaming JSON output -


I have a simple node. I am trying to create a JS based streaming API. What I want to do is just as if I kill the server URL, the output should stream a set of test data (JSON) as the Twitter Streaming API.

  requires the app = ('Express') (); Var requires server = ('http'). Server (app); Var io = Requirement ('socket.io') (server); Server.listen (8083); App.get ('/', function (rik, ridge) {res.write (io.on ('connection', function (socket) {socket.emit ('item', {hello: 'world'});} ));});  

So, if I do curl http: // localhost: 8083 / , then I want some such output:

  

I'm new to node. JS and web socket can be very wrong on the basics of how the node works, I know the best solution. First, it is better to put the JSONSTream part into a middleware such as:

Requires var _ = ('bell'); // https://github.com/smurthas/Express-JSONStream/blob/master/index.js Function jsonstream (bytes) {jsonstream data out via return function jsonstream (req, res, next) {// GET) To request first = true; Var nop = function () {}; Res.jsonstream = function (object, f) {f = _.is function (f)? F: nop; If (! (Objects of object and object object)) {return f (); } Try {if (first) {first = false; Res.writeHead (200, {'Content-Type': 'Application / Jason', 'Cash-Control': 'No-Cash', 'Connection': 'Keep-alive'}); } Res.write (JSON.stringify (object) + '\ n'); } Hold (error) {return _.defer (f.bind (faucet, mistake)); } F (); }; the upcoming(); }; }

Again, suppose you want to be notified via this API when someone joins the socket.

  var app = is required ('Express') (); Var requires server = ('http'). Server (app); Var io = Requirement ('socket.io') (server); Var _ = is required ('trash'); Var EventEmitter = Required ('Event'). EventEmitter; Server.listen (8083); Var mediator = new event ammeter (); Io.on ('Connection', Function (Socket) (Mediator.mate ('Io: Connection: New', Socket);}); // Specify an array of other parameters, Middleware, // Here we are already Define JSSSstream app .Get ('/', [JasonStream ()], Function (Rick, Ridge) {Function OnNunction (Socket) {Ridge}. {Junkstream ({Type: 'New Connection', Message: 'A New connection found ', socket: {id: socket.id}})}} Bind new connections to the mediator, let us Must have access to a fast gateway / because socket.io does not provide a good implementation of "removeListener" in 1.1.0 / / every time this will connect to a socket.io // in the current route stream mediator Uninstall 'New Connection Connection' from the mediator / user when the user disconnects' connection '(function) (intermediary .removeListener (' Connection ', onNewConnection;}); res .jsonstream ({type: 'Welcome', message: 'waiting for connection'}}}}); Finally, if you want to test this code without connecting to socket.io then use the following simulator:  
  // Simulate Connection using socket.io moderator (function simulation) {var dummySocket = {id: ~~ (Math.random () * 1000)}; moderator. Emit ('Io: connection: new', dummySocket); setTimeout (Simulation, math.rendum) * 1000);}) ();  

Comments