How do I call into libp2p rather than having it call my functions in a loop? #2024
-
Hi! I'm trying to use libp2p in conjunction with an rpc library, so libp2p isn't the main point where requests arrive. However, in the tokio chat example, it seems to be necessary to continiously loop over event = swarm.next() to drive? libp2p. Is there a different way to submit that call to tokio, or am I required to create a separate thread which has the same kind of loop as in the tokio chat example, which communicates with the rest of my program through channels? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
It doesn't have to be a separate thread. What I've found to work reasonably well is to use the The naming might be bit odd but the way it works is that we have a thing called Another thing that might be interesting in our use of An alternative way of doing things is to wrap the |
Beta Was this translation helpful? Give feedback.
It doesn't have to be a separate thread. What I've found to work reasonably well is to use the
tokio
orfutures
select!
macro to poll multiple futures simultaneously. You can see an example of this here:https://github.com/comit-network/xmr-btc-swap/blob/475aac1387846c8e8351bcd4d3bef4a7e019e806/swap/src/protocol/bob/event_loop.rs#L89-L193
The naming might be bit odd but the way it works is that we have a thing called
EventLoop
that has ownership of theSwarm
and c…