Consider using channels instead of callbacks
Closed this issue ยท 5 comments
I've found myself implementing channels on top of callbacks to process peer connection status changes and messages. Channels may be a bit more flexible and convenient for Rust users.
As an example, the proposal may result in the following API:
let peer_connection = RtcPeerConnection::new(...);
let rx = peer_connection.on_gathering_state_change();
// Wait until all the ice cadidates are gathered.
while let Some(event) = rx.next().await? {
if event == GatheringState::Complete {
break;
}
}
// Get local description.
let current_session_description = peer_connection.local_description();
That could be interesting to have a channel-based API indeed.
However it should come as a separate module as this crate is meant to have an API similar to regular datachannels (js and libdatachannel).
Also to be able to get potentially multiple rx
it would require to use some kind of mpmc channels which are not in the std and would require a dependency (of the public API) on an external crate such as crossbeam
.
That make total sense. It will also add dependency on some async runtime. Thanks for elaboration.
Still I keep that in mind and I'll explore it at some point.
I hacked together a little async wrapper for this crate at https://github.com/wngr/async-datachannel. I guess it's a bit opinionated at the moment for my use-case. However, if you're interested in this @lerouxrgd, I could try to assemble a PR to get it merged into this crate based on your feedback.
Thanks for the wrapper, it's a good reference indeed ! I mentioned it in the README.md
so that other people can find it too.