Skip to content

Communicating between a libp2p thread and a UI thread #3812

Closed Answered by thomaseizinger
pieterdd asked this question in Q&A
Discussion options

You must be logged in to vote

Right, a Receiver implements Stream and so does Swarm. Meaning, via the StreamExt extension trait, you can call .next() on it and do:

futures::future::select(receiver.next(), swarm.next()).await

This will give you an Either where Either::Left contains a tuple of: (Some(command), unfinished_swarm_future) and Either::Right contains a tuple of (Some(swarm_event), unfinished_receiver_future).

Both, Receiver and Swarm return cancellation-safe futures, meaning you can just drop the unfinished futures, i.e. do nothing with them.

Here is a good example from a workshop I did recently: https://github.com/thomaseizinger/libp2p-workshop/blob/iteration-4/src/event_loop.rs

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
4 replies
@pieterdd
Comment options

@thomaseizinger
Comment options

Answer selected by pieterdd
@pieterdd
Comment options

@thomaseizinger
Comment options

Comment options

You must be logged in to vote
1 reply
@pieterdd
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants