How to handle ``on("message"`` for both binary and json messages
See original GitHub issuePrerequisites
- 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:
- Created a year ago
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
socket.onmessage
is made for Web API compatibility, so it returns theEvent
like object. https://github.com/websockets/ws/blob/master/lib/event-target.js#L193-L200socket.on('message')
is the actual NodeEventEmitter
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.@climba03003 thanks for explaining! that’s probably what I was missing 😃