Skip to content

createWebSocketStream - objectMode #1640

@tadeuszwojcik

Description

@tadeuszwojcik

Hi, forgive me please if it's silly question, but is there a reason why duplex stream created via createWebSocketStream doesn't have objectMode set to true? ws.onmessage clearly produces/pushes separate messages not continuous stream of bytes, am I missing something?
I'm trying to run smth like this:

const WebSocket = require('ws')
const ws = new WebSocket('wss://www.bitmex.com/realtime')
const messageStream = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })

ws.once('open', function open() {
  ws.send(JSON.stringify({ op: 'subscribe', args: ['orderBookL2'] }))
})

async function run() {
  for await (let message of messageStream) {
    console.log(message)
  }
}
await run()

Is such pattern fine? Seems to be working and each message is correct string that matches json object provided by the server, but could that break as objectMode is not set?
I also tried to send message via stream, but couldn't sort it out quickly.

Would you perhaps accept PR that would change https://github.com/websockets/ws/blob/master/lib/stream.js#L65:
to:

const duplex = new Duplex({
    autoDestroy: false,
    emitClose: false,
    objectMode: false,
    readableObjectMode: false,
    writableObjectMode: false,
    ...options
  });

so I could provide objectMode flag via options?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions