Hi,
I'm not really sure if it is a bug or a feature but I'm wondering why when sending data of any types (String, Buffer, TypedArrayView, ArrayBuffer, or Blob) I don't get the exact same type on the "receiving" part.
For example, if I send a simple string from computer A (the sender) like:
p.send("Hello World!")
On computer B (the receiver), for this code:
p.on('data', function (data) {
console.log('data: ' + data)
console.log(data)
})
I see, in the Chrome console:
data: Hello World!
[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]
So, when concatened to a string, it is displays as a string, but when displayed "as-is", it is an Uint8Array.
But I need to get the exact type I sent, that is, if I call typeof data on computer B I should get the correct type I sent and not Uint8Array. This is the behavior I have when creating the dataChannel "from scratch" without simple-peer. So I expected simple-peer to behave in the same manner.
Is there any way or option available to get the correct object sent?
Regards,
M.
simple-peer is modeled on the Node.js net API, which treats remote peers as a stream. Stream 'data' events聽normally emit objects of type Buffer (which is a subclass of Uint8Array).
However, you can switch the stream into Object Mode if you want to have the objects that come across the data channel not get converted into Buffer objects. See https://nodejs.org/api/stream.html#stream_object_mode to learn more about Object Mode.
I will also add tests to ensure that this behavior continue to work and doesn't regress in a future version.
Actually, in trying to add tests for this, I remembered why it isn't always possible to preserve the type. The receiver specifies what type they want binary data to arrive in, and there are only two options: ArrayBuffer and Blob. Internally, we choose ArrayBuffer and trivially wrap those in a Buffer, since that's what all Node.js streams emit.
So... if you want to preserve strings at the very least, you can put the stream into objectMode, but other binary types will all be converted to Buffer. This a limitation of WebRTC itself.
If you need to distinguish among binary types for some reason, you should send across metadata that tells the receiver how to unwrap the data into the right type.
More info: https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/binaryType
Hi feross,
Thanks a lot for your quick answer, that's exactly what I needed!
In my case, I had to distinguish incoming data between String and ArrayBuffer. So by using objectMode, I can at least test the type so that's just fine.
It may be useful to document this option of the SimplePeer constructor. I may do a quick PR for that if I find the time.
Thanks again for your time and help!
p.on('data',data=>{
let str=new TextDecoder("utf-8").decode(data)
console.log(str);
})
If you want send objects ,make use of JSON.stringify() and JSON.parse() functions.
Most helpful comment
p.on('data',data=>{
let str=new TextDecoder("utf-8").decode(data)
console.log(str);
})
If you want send objects ,make use of JSON.stringify() and JSON.parse() functions.