Skip to content

Add readableObjectMode as an option for createWebSocketStream #185

@aelnaiem

Description

@aelnaiem

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

Adding readableObjectMode as an option for fastify-websocket.

I noticed that currently in the documentation, it says that fastify-websocket supports objectMode as an option for [ws](https://github.com/websockets/ws/), but objectMode is not listed in the ws options documentation.

It seems reasonable to add readableObjectMode as an option to fastify-websocket as it is an option for createWebSocketStream and achieves a similar goal. It may also make sense to allow users to change other connection (duplex) options like readableHighWaterMark and writableHighWaterMark, but I haven't needed those options yet.

Motivation

There is currently no way to set readableObjectMode and this can lead to a TypeError for certain WebSocket applications, specifically:

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of 
Buffer or Uint8Array. Received an instance of ArrayBuffer

That being said, this issue which led to the inclusion of readableObjectMode as an option for createWebSocketStream suggests there are issues with objectMode and backpressure handling, and the option could be a footgun.

This error also didn't occur when using express-ws so I wonder if it's related to how Fastify works as the issue also highlights a bug in Node.js core. I noticed that express-ws doesn't seem to call createWebSocketStream at all and so I'm curious why there is a difference in implementation and that might reflect if this feature is needed.

Example

fastify.register(require('fastify-websocket'), {
  options: { maxPayload: 1048576 },
  connectionOptions: { readableObjectMode: true } // can include other duplex options 
})

Then these options can be passed to WebSocket.createWebSocketStream

const connection = WebSocket.createWebSocketStream(socket, opts.connectionOptions)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions