Search code examples
node.jsmongodbsocket.iomongoosederbyjs

MongoDB document streaming with HTTP response?


After an all day research on node.js real-time frameworks/wrappers (derby.js, meteor, socketIO...) I realised, that the more old-fashioned (sorry) way of a restful API fits all my needs.

One of the reasons I thought I have to use an ongoing socket connection was because I want to stream my MongoDB documents from the database instead of loading them all into memory on the server. I think this is the recommended way because it minimizes the use of server ressources.

But here is the problem:

Does a simple document query streaming work with the ordinary HTTP request/response model or do we have to establish an ongoing socket-connection to stream all documents to the client?

Note: I only have to load the documents on an ajax call - without the need to have new documents to be pushed to the client (so really no need to be realtime).

Is there anything special to consider?


Solution

  • You can stream the results of the query using the standard HTTP request/response APIs.

    The general sequence of calls is:

    res.writeHead(<header content>)
    res.write(<data>)
    ...
    res.write(<data>)
    res.end();
    

    But you make those calls asynchronously, driven by the streaming events from your query.