Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News A New Streaming API for Node.js

A New Streaming API for Node.js

Leia em Português

This item in japanese

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.

Rate this Article