-
Notifications
You must be signed in to change notification settings - Fork 15
Description
Issue: Browser WebSocket Converts Binary Data to Blob
Description
When using @open-rpc/client-js with a WebSocketTransport in a browser environment, binary data sent from the server is received as a Blob. This behavior causes issues when trying to process the payload in resolveResponse with JSON.parse(payload), as Blob cannot be directly parsed into JSON.
In contrast, when using the library in a Node.js environment, the WebSocket receives binary data as a Buffer, which can be directly passed to JSON.parse to obtain the desired object.
Expected Behavior
The library should handle WebSocket messages consistently across browser and Node.js environments. Binary data sent from the server should be correctly converted into a string or JSON object, regardless of whether the client is running in a browser or Node.js.
Actual Behavior
In the browser:
- Binary data sent from the server is received as a
Blob. - The
Blobcannot be directly processed byJSON.parse.
In Node.js:
- Binary data is received as a
Buffer. - The
Buffercan be successfully processed byJSON.parse.
Steps to Reproduce
- Use
@open-rpc/client-jswithWebSocketTransportin a browser. - Connect to a WebSocket server that sends JSON-RPC notifications as binary data.
- Observe that the incoming data is a
Bloband cannot be processed byJSON.parse.
Proposed Solution
- Modify the
WebSocketTransportimplementation to detect when incoming messages areBlobobjects in the browser. - Convert the
Blobto a string usingBlob.text()or a similar method before passing it toJSON.parse.
Environment
@open-rpc/client-jsversion: 1.8.1- Browser: Brave Version 1.73.105 Chromium: 131.0.6778.265 (Official Build) (64-bit)
- Node.js version: 22.11
- Operating System: Ubuntu 24.04.1 LTS
Additional Context
This discrepancy between browsers and Node.js arises because the browser WebSocket implementation defaults to converting binary data into Blob, while Node.js treats it as a Buffer.
It would be great if the library could handle this scenario internally to ensure consistent behavior across environments.