BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

A New Streaming API for Node.js

| by James Campos on Jan 21, 2013. Estimated reading time: 1 minute |

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

Adoption Stage
Style

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

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and dont miss out on content that matters to you

BT