paullouisageneau / datachannel-wasm

C++ WebRTC Data Channels and WebSockets for WebAssembly in browsers
MIT License
148 stars 25 forks source link

Using Multithreading #52

Closed tbuchs closed 5 months ago

tbuchs commented 5 months ago

Hi, I am using your library and it works great. However, I have the problem, that my performance in the communication part is quite slow. I believe that the reason is, that I only use the "main thread" for sending and the rest of the program is multithreaded and therefore needs to wait for the sending. Is there a way, that sending also works with different threads? When I try to send on another thread than the main thread I get Pthread 0x00601ec0 sent an error! TypeError: can't access property "readyState", dataChannel is undefined.

Thanks for your help!

paullouisageneau commented 5 months ago

However, I have the problem, that my performance in the communication part is quite slow. I believe that the reason is, that I only use the "main thread" for sending and the rest of the program is multithreaded and therefore needs to wait for the sending. Is there a way, that sending also works with different threads?

There is no way to call the WebRTC API concurrently from multiple threads, as wasm threads are implemented with web workers, and the WebRTC API is not exposed to workers. Of course, it would be possible to polyfill it by forwarding method calls to the event loop, but that would defeat the purpose.

However, this should not be a performance bottleneck as you shouldn't need to wait for the sending to happen, you should just pass the message to the main thread without blocking. The underlying JS call itself doesn't wait for the actual sending to happen.

tbuchs commented 5 months ago

Thanks, for the detailed answer - that makes sense now. I‘ll have a closer look on my passing to the main thread.

I‘ll close that as completed.