BT
x Your opinion matters! Please fill in the InfoQ Survey about your reading habits!

A New Streaming API for Node.js

by James Campos on Jan 21, 2013 |

Node.js, the server-side JavaScript platform built on top of Google’s V8 engine, has included a new Streaming API in its unstable 0.9.4 release. Nicknamed "streams2", it is scheduled to be included in the 0.10 stable release.

The new API protects against data loss by starting streams in a paused state, and waiting until a listener is attached before emitting ‘data’ events. Additionally, the paused state is now guaranteed to not emit any ‘data’ events, instead of being merely advisory. A new read() method is included, which allows an arbitrary number of bytes to be read from the stream, leaving the remaining for another program to consume.

The older methods and events are kept for backwards compatibility:

Readable streams switch into "old mode" when a 'data' event handler is added, or when the pause() or resume() methods are called. The effect is that, even if you are not using the new read() method and 'readable' event, you no longer have to worry about losing 'data' chunks.

An edge case is introduced - if no ‘data’ event handler is added, and the pause() and resume() methods are never called, the stream will never emit an ‘end’ event, and will remain paused forever.

A new Streams implementation has been discussed since the 0.4 release, but more pressing problems were always at hand, and it was considered too difficult to make backwards-compatible, required to ensure adoption. In 0.10, the bullet has finally been bit, and developers are encouraged to try it out and provide feedback.

Node.js is designed for building fast, scalable network applications, is developed by Joyent, Inc, as well as the open-source community, and is used by companies such as Microsoft, LinkedIn, and eBay.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT