question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to handle ``on("message"`` for both binary and json messages

See original GitHub issue

Prerequisites

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

Issue

Hello,

I’m having an issue where I’m sending both json strings as well as flatbuffer/uin8 arrays to my websocket connection.

In on("message" I’m calling toString but that doesnt work for the Uint8Arrays being sent. How should I handle both message types / is there a flag to enable?

Current version: 4.2.2

error when calling toString on a message that is actually an Uint8Array

SyntaxError: Unexpected token ¶ in JSON at position 0
    at JSON.parse (<anonymous>)
    at C:\git\needle-tiny-networking-package\packages\websocket\src\networking.js:52:28
    at WebSocket.<anonymous> (C:\git\needle-tiny-networking-package\packages\websocket\src\proxy.js:31:17)
    at WebSocket.emit (events.js:388:22)
    at Receiver.receiverOnMessage (C:\git\needle-tiny-networking-package\packages\websocket\node_modules\fastify-websocket\node_modules\ws\lib\websocket.js:1137:20)
    at Receiver.emit (events.js:376:20)
    at Receiver.dataMessage (C:\git\needle-tiny-networking-package\packages\websocket\node_modules\fastify-websocket\node_modules\ws\lib\receiver.js:513:14)
    at Receiver.getData (C:\git\needle-tiny-networking-package\packages\websocket\node_modules\fastify-websocket\node_modules\ws\lib\receiver.js:446:17)
    at Receiver.startLoop (C:\git\needle-tiny-networking-package\packages\websocket\node_modules\fastify-websocket\node_modules\ws\lib\receiver.js:148:22)
    at Receiver._write (C:\git\needle-tiny-networking-package\packages\websocket\node_modules\fastify-websocket\node_modules\ws\lib\receiver.js:83:10)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at Receiver.Writable.write (internal/streams/writable.js:303:10)
    at Socket.socketOnData (C:\git\needle-tiny-networking-package\packages\websocket\node_modules\fastify-websocket\node_modules\ws\lib\websocket.js:1231:35)
    at Socket.emit (events.js:376:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
climba03003commented, Apr 14, 2022

socket.onmessage is made for Web API compatibility, so it returns the Event like object. https://github.com/websockets/ws/blob/master/lib/event-target.js#L193-L200

socket.on('message') is the actual Node EventEmitter implementation. It receive the raw data (e.g. Buffer, ArrayBuffer, etc).

The ws document already give you the hint on how to handle your case.

socket.on('message', function(data, isBinary) {
  // it is sent with binary
  if(isBinary) {
    const buf = Buffer.from(data)
  } else {
    const json = JSON.parse(Buffer.from(data).toString())
  }
})
0reactions
marwiecommented, Apr 14, 2022

@climba03003 thanks for explaining! that’s probably what I was missing 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Binary Data in JSON String. Something better than Base64
In JSON, control chars, " and \ are not allowed to appear in a string. So the binary data would require some transformation...
Read more >
JSON and Binary Topics for Real-Time Messaging
Read our latest blog post for a full breakdown on how to leverage JSON and Binary Topics for real-time messaging using Diffusion and ......
Read more >
How to transfer text and binary data in one connection? #20
The API works with three commands: Deliver file information in json format. {"action":"create", "filename":"test.png"} - ...
Read more >
Don't Use JSON And XML As Internal Transfer Formats
Yes, JSON is less verbose than XML, but it's still a text format that you don't need. Instead, in most cases you'd better...
Read more >
Beating JSON performance with Protobuf - Auth0
Protobuf, the binary format crafted by Google, surpasses JSON performance even on JavaScript environments like Node.js/V8 and web browsers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found