gfodor / p2pcf

Low cost, low effort P2P WebRTC serverless signalling using Cloudflare Workers
MIT License
952 stars 53 forks source link

Why can't you send string using p2pcf.send #22

Open silenceofthewans opened 1 year ago

silenceofthewans commented 1 year ago

Just trying to learn, Is there a reason the send method only supports ArrayBuffer. I know I can convert string to ArrayBuffer with TextEncoder and vice versa but it's additional steps since I only need to send simple two characters messages. Is it more performant than sending strings directly? My use case is sending simple two character string with lowest latency possible.

jonathanhawleypeters commented 1 year ago

Hi @silenceofthewans,

The choice to use ArrayBuffer is at the WebRTC specification level, @gfodor will not be able to change that for you. You can see a draft of the WebRTC specification here https://w3c.github.io/webrtc-charter/webrtc-charter.html, and join the W3C here to discuss it.

If you choose to work with the constraint, you can compare the performance of converting the messages to ArrayBuffer ahead of time to encoding them as messages are sent (I'm assuming that all possible values are known to you). I suspect you will not see much of a difference, but you can't know without testing.

Hope that helps.

Edit: I was mistaken, see the following comment.

evan-brass commented 1 year ago

Perhaps this comment needs an update?

I don't know the details of P2Pcf, or simple-peer, but WebRTC DataChannels can send strings, typed arrays, or blobs.

WebRTC has two (actually 4) data message types: string or binary. Receiving a string message results in a js string as the data property, while receiving a binary message will produce either an ArrayBuffer or Blob based on the binaryType property set by the receiver.

If there's a constraint to only ArrayBuffers, I think it's higher in the stack than the WebRTC API.