Skip to content

Instantly share code, notes, and snippets.

Last active May 3, 2018
What would you like to do?

Streaming Data with Fetch() and NDJSON

*"If you stream it, you can do it" -- Walt Disney1 *

Streams are trickling into the scene as we search for ways to improve performance. What if instead of waiting for our entire ajax response to complete, we could start showing the data as it arrives?

Streams allow us to do this. They are a data source that can be created and processed incrementally. This means as chunks of data become available, we are able to do work on them right away.

Using the Fetch API with a data format called NDJSON that breaks larger JSON objects into smaller JSON objects delimitated by newline characters, we are able to receive a stream of smaller chunks of JSON data as a stream. As our NDJSON data is streaming in, we can start processing and rendering right away. This makes users happy because they are able to see things sooner and developers happy because it increases overall performance. If you tie all of this in with service workers, then you can really see the improvements in performance.

incremental NDJSON rending vs. JSON object

3. Example Usage

As seen below, you can use fetch() with an endpoint that sends NDJSON to start manipulating and rendering that data line-by-line as you receive it.

streaming json process

import ndjsonStream from 'can-ndjson-stream';

const fetchNdjson = async () => {
  const response = await fetch('/api');
  const exampleStream = ndjsonStream(response.body);

  let result;
  while (!result || !result.done) {
    result = await exampleStream.getReader().read();
    console.log(result.done, result.value);

Sounds like a win-win, but what is the catch? Well, there are packages out there on npm such as ndjson that serializes JSON to NDJSON, but we don’t have an easy way to deserialize NDJSON to a stream of JavaScript objects… until now!

Introducing can-ndjson-stream

can-ndjson-stream is a simple JavaScript module that does the heavy lifting of serializing your NDJSON stream into a ReadableStream of JavaScript objects. Use this just as you would JSON.parse with a JSON object.

ndjson streaming db row

Follow these simple steps to use the can-ndjson-stream module.

//1. require the can-ndjson-stream module
import ndjsonStream from 'can-ndjson-stream';

//2. pass the NDJSON stream response body to the ndjsonStream function. Typically, this would be used with the fetch() method.

const readableStream = ndjsonStream(response.body);  
//3. use readableStream, which is now a ReadableStream of JS objects, however you like. The ReadableStream API exposes a .getReader() and .cancel() method.


Getting Started

1. Download the package from npm and save it to your project.

npm i can-ndjson-stream --save

2. Import the module as you would a typical npm module at the top of each file.

Require var ndjsonStream = require('can-ndjson-stream');

-- OR --

ES6 import import ndjsonStream from 'can-ndjson-stream';

3. Parse your response.body using the ndjsonStream function and do work on the result.

Once you parse your response.body, you can interact with your ReadableStream by read'ing it like this: exampleStream.getReader().read(). That will return a promise that resolves to one line of your NDJSON.

Using Async/Await

import ndjsonStream from 'can-ndjson-stream';

const fetchNdjson = async () => {
  const response = await fetch("/api");
  const exampleStream = ndjsonStream(response.body);

  let result;
  while (!result || !result.done) {
    result = await exampleStream.getReader().read();
    console.log(result.done, result.value); //result.value is one line of your NDJSON data

Using promises

import ndjsonStream from 'can-ndjson-stream';

fetch('/api')  // make a fetch request to a NDJSON stream service
.then((response) => {
	return ndjsonStream(response.body); //ndjsonStream parses the response.body

}).then((exampleStream) => {
	let read;
	exampleStream.getReader().read().then(read = (result) => {
		if (result.done) return;

		exampleStream.getReader().read().then(read); //recurse through the stream

4. [Optional] Create a simple API to serve sample NDJSON data.

Follow this tutorial on the Bitvoi blog that gets you started or take a look at the demo in the can-ndjson-stream repo.

What next?

If you enjoyed this article, tweet to us how you plan to use this module! Also check out the rest of the CanJS library. If you need any help, please don’t be afraid to leave a comment below or ask questions in the CanJS Gitter or forums!

Inspirational Quotes about Streams Through History1

If not us, who? If not now, when? -- John F. Kennedy

Hope is a waking stream. -- Aristotle

Life is trying things to see if they work. -- Ray Bradbury


  1. may or may not be accurate. 2

Copy link

jeffposnick commented Jun 20, 2017

Great topic to write about, and I'm glad that there are libraries like can-ndjson-stream out there! My only comments:

  • My inclination is to go with async/await syntax nowadays, as I find it more readable than chaining .then()s.
  • It would be great to mention the guarantees around the deserialized result.value inside the reader block. Is it always a single entry corresponding to a line in the JSON data, or is it an array that might include multiple entries?

You could also loop in @jakearchibald to see if he had any comments!

Copy link

bgando commented Jul 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment