adnan kamili
adnan kamili

Reputation: 9455

Expressjs - stream cassandra result set to client

I have to stream the json resultset stream returned by cassandra to the browser. I got it working using express-stream but that involved stringifying the each row and and passing it to res.pipe()

I am wondering why following didn't work:

app.get('/api/v1/streamevents', function (req, res) {
   let cstream = client.stream(selectQuery, ['339aa84a-4bba-411f-a4fb-38167a987cd2'],options)
   cstream.pipe(res)

})

I get following error:

TypeError: First argument must be a string or Buffer
    at ServerResponse.write (_http_outgoing.js:458:11)
    at ResultStream.ondata (_stream_readable.js:555:20)
    at emitOne (events.js:96:13)
    at ResultStream.emit (events.js:188:7)
    at ResultStream.Readable.read (_stream_readable.js:381:10)

Upvotes: 0

Views: 351

Answers (1)

Sergey Lapin
Sergey Lapin

Reputation: 2693

As per docs for the stream method of Cassandra driver:

Returns a Readable Streams2 object in objectMode

At the same time, underlying ServerResponse.write only accepts either String or Buffer as a data chunk when sending to the client, not Object.

That's why it works when each row is stringified.

You would have to use a Transform stream in the middle to convert the selected cell values to string.

Upvotes: 2

Related Questions