Implementing AI Streaming Responses with JSON Lines Chunked Communication Instead of SSE

javascript dev.to

jsonl-webstream

Lightweight library for JSON Lines web stream between browsers and Node.js environments

Overview

This library provides utilities for processing JSON Lines formatted data through the Web Streams API It enables efficient streaming of JSON Lines data with minimal memory overhead across browsers and Node.js environments.

Installation

npm install jsonl-webstream
Enter fullscreen mode Exit fullscreen mode

Usage

Reading JSON Lines

import { createJsonLinesReceiver } from 'jsonl-webstream';

// With fetch API
async function processJsonLines() {
  const response = await fetch('https://example.com/stream');
  const reader = response.body.getReader();
  const jsonlStream = createJsonLinesReceiver(reader);

  for await (const jsonData of jsonlStream) {
    // Process each JSON object
    console.log(jsonData);
  }
}
Enter fullscreen mode Exit fullscreen mode

Writing JSON Lines

import { createJsonLinesSender } from 'jsonl-webstream';
function handleRequest(reply) {
  // Create a JSON Lines writer and its associated stream
  const {
Enter fullscreen mode Exit fullscreen mode

Source: dev.to

arrow_back Back to Tutorials