Browse files

writable section complete

  • Loading branch information...
1 parent 1d9daa1 commit decba93fa96e15c25faad69f0ed579025a03fbb6 @substack committed Jun 27, 2013
Showing with 49 additions and 1 deletion.
  1. +1 −0 example/basics/writing0.js
  2. +8 −0 example/basics/writing1.js
  3. +40 −1 readme.markdown
View
1 example/basics/writing0.js
@@ -0,0 +1 @@
+process.stdout.write('beep ');
View
8 example/basics/writing1.js
@@ -0,0 +1,8 @@
+var fs = require('fs');
+var ws = fs.createWriteStream('message.txt');
+
+ws.write('beep ');
+
+setTimeout(function () {
+ ws.end('boop\n');
+}, 1000);
View
41 readme.markdown
@@ -401,7 +401,11 @@ your own line-parsing logic.
## writable streams
-A writable stream is a stream you can `.pipe()` to.
+A writable stream is a stream you can `.pipe()` to but not from:
+
+``` js
+src.pipe(writableStream)
+```
### creating a writable stream
@@ -441,6 +445,41 @@ into `Buffer`s unless you create your writable stream with
If the readable stream you're piping from writes objects, create your writable
stream with `Writable({ objectMode: true })`.
+### writing to a writable stream
+
+To write to a writable stream, just call `.write(data)` with the `data` you want
+to write!
+
+``` js
+process.stdout.write('beep boop\n');
+```
+
+To tell the destination writable stream that you're done writing, just call
+`.end()`. You can also give `.end(data)` some `data` to write before ending:
+
+``` js
+var fs = require('fs');
+var ws = fs.createWriteStream('message.txt');
+
+ws.write('beep ');
+
+setTimeout(function () {
+ ws.end('boop\n');
+}, 1000);
+```
+
+```
+$ node writing1.js
+$ cat message.txt
+beep boop
+```
+
+If you care about high water marks and buffering, `.write()` returns false when
+there is more data than the `opts.highWaterMark` option passed to `Writable()`
+in the incoming buffer.
+
+If you want to wait for the buffer to empty again, listen for a `'drain'` event.
+
## classic streams
Classic streams are the old interface that first appeared in node 0.4.

0 comments on commit decba93

Please sign in to comment.