Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

316
Views
Unable to consume ndjson stream in Node, but similar code works in React

I am trying to consume data from this endpoint on lichess.org.

Here is a minimal working example of a React component consuming that stream of data. I'm using a library called can-ndjson-stream.

import ndjsonStream from "can-ndjson-stream"
import { useEffect } from "react"

function App() {
  useEffect(() => {
    fetch("https://lichess.org/api/tv/feed")
      .then(res => ndjsonStream(res.body))
      .then(stream => {
        const streamReader = stream.getReader()
        streamReader.read().then(async res => {
          while (!res || !res.done) {
            res = await streamReader.read()
            console.log(res.value)
          }
        })
      })
      .catch(console.error)
  }, [])
  return <>Lorem Ipsum</>
}

export default App

However, if I try to write this same code and run it in Node like this:

import ndjsonStream from "can-ndjson-stream"
import fetch from "node-fetch"

fetch("https://lichess.org/api/tv/feed")
  .then(res => ndjsonStream(res.body))
  .then(stream => {
    const streamReader = stream.getReader()
    streamReader.read().then(async res => {
      while (!res || !res.done) {
        res = await streamReader.read()
        console.log(res.value)
      }
    })
  })
  .catch(console.error)

I get this error:

ReferenceError: ReadableStream is not defined at ndjsonStream

So it seems like the res from the fetch is null or undefined, but fetching other APIs works fine.

I also tried using axios instead of node-fetch like this:

import ndjsonStream from "can-ndjson-stream"
import axios from "axios"

axios
  .get("https://lichess.org/api/tv/feed")
  .then(res => ndjsonStream(res.data))
  .then(stream => {
    const streamReader = stream.getReader()
    streamReader.read().then(async res => {
      while (!res || !res.done) {
        res = await streamReader.read()
        console.log(res.value)
      }
    })
  })
  .catch(console.error)

But it just hangs and shows no output. Thanks to anyone that could shed some light on this or offer any alternative way to run this in Node.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Thanks to the comments from tromgy, I was able to make something that works. I went with the library hyperquest to help handle the request and piping of the stream. I also used the ndjson library.

Here is some working code:

hyperquest("https://lichess.org/api/tv/feed")
    .pipe(ndjson.parse())
    .on("data", console.log)

Note that you can manipulate the objects as they arrive with the second argument of on(), like below.

...
.on("data", (obj) => {
    foo(obj)
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!