Friday, 15 April 2011

logging - Understanding the WriteStream in Node.js -


Recently I wrote a small multi-room chat application that was using Node.js and Socket.IO And I log in the messages sent to different files based on the room they came in. At the moment I am using several writing streams (one in each room) to store this functionality, but when it works, I was wondering how to actually use the writing streams is.

My concerns:

  1. When I implemented the first writing stream, it seemed to be a perfect match instead of writing the whole file, whenever someone posted a new post I used to go - although retrospective I'm not sure that it is ideal to open 10-20 streams all the time.
  2. I currently use the .end () function when the end user leaves a room, but on one side of the callback function I can not use it, there is no way to say that All the messages have already been called (finally calling), which brings me back with 1. And the question if I am using the writing streams, because it is done to use it

I think my question is ... should write / write serials In such cases can be used for admission purposes? Do they throttle resources while waiting for input, or does it open large numbers for extended periods of time?

With respect to point 2 I can be quick: when you .end () < Call / code> on a writing stream, the stream no longer accepts new writes, but it completes writing the data file system. You do not have to worry about losing part of your chat log - unless there's an error (for this, listen to the stream 'error' event).

And regarding the point 1: I do not believe there is anything wrong with opening the file system stream for a long time. Many applications do this, for example, to write your logs. One potential problem I see with your setup is that, as much as your chat system is managing, so much data increases, you can quickly generate chat logs, you can write them on the file system . If this happens, then your stream will start to buffer more data and in the end your process will be out of memory and accident. Of course it depends on your data versions.


1 comment: