3 ms·
> My API just returned a single JSON array, so the server couldn’t send anything until it had collected all results. Why can't you stream a JSON array? Edit:
by jwalton 7y ago
> My API just returned a single JSON array, so the server couldn’t send anything until it had collected all results.
Why can't you stream a JSON array?
Edit: Here's a (hastily created and untested) node.js example, even:
class JSONArrayStream extends Transform {
constructor() {
super({readableObjectMode: false, writableObjectMode: true});
this.dataWritten = false;
}
_transform(data, encoding, callback) {
if (!this.dataWritten) {
this.dataWritten = true;
this.push('[\n');
this.push(JSON.stringify(data) + '\n');
} else {
this.push(',' + JSON.stringify(data) + '\n');
}
callback();
}
_flush(callback) {
this.push('\n]');
}
}
- stephenr 7y agoyou’d probably need to do some tricks to get it to parse in a browser. Editing, because I can’t reply: I was specifically going to mention SSE/EventSource but expected an immediate “not everything is a browser” response. Editing the 2nd: yep, that’s kinda what I meant by “tricks” - essentially splitting out chunks to pass to the json parser.
- jwalton 7y agoSee my edited example above; you can stream data to this, it'll stream nicely to the browser. This uses "\n"s at the end of every line, which means you can write a very simple streaming parser client side, because you can just split the input at "\n," to get nice JSON bits, but there's certainly JSON streaming libraries on NPM that will parse this for you more "properly". And, it parses with a normal JSON parser too.
- pbedat 7y agoModern browsers understand text/event-stream so no need for dirty tricks ;)
- pbedat 7y agoAlso: Server sent events
- spenczar5 7y agoThere can be a bit more to it: - how can a client send an error message if it runs into problems mid-stream? You can invent a system, but you’re walking into an ad-hoc protocol pretty fast; why not use something others wrote? - what if the remote end wants to interrupt the stream sender to say “stop sending me this” for any reason? For example, an erroneous item in the stream, or a server closing down during a restart. - grpc supports fully bidirectional streams, interleaving request and response in a chatty session; how do you do this? Not that the original article mentioned these. I bristle though when I hear the engineer’s impulse to “why don’t you just”-away at something.