Struggling with libp2p and async/await

Hi, I’m trying to implement a libp2p application which utilizes sqlx async functions to access a database for storage, and which should allow interaction (via stdin plus later web).
The NetworkBehaviorEventProcess inject_event() methods aren’t async, so I can’t call my database code from there. The main loop (copied from the peer to peer chat example) does the async poll stuff by itself, and also cannot call async functions. Is there some standard way of running and interacting with the libp2p loop along other async tasks? In my experiments I failed most of the time on ownership and lifetime issues with streams and didn’t find a way to express what I want in a natural way.

Hi @hamamo,

The NetworkBehaviorEventProcess inject_event() methods aren’t async, so I can’t call my database code from there.

:+1: I would use NetworkBehaviourEventProcess for very minimal conversion logic only, or even go as far as using event_process = false (see e.g. Make event_process = false the default by thomaseizinger · Pull Request #2214 · libp2p/rust-libp2p · GitHub).

Is there some standard way of running and interacting with the libp2p loop along other async tasks?

Yes. The example below should illustrate such a use-case. Your async database logic would live either in main directly, or some function called from the async main function.

In my experiments I failed most of the time on ownership and lifetime issues with streams and didn’t find a way to express what I want in a natural way.

I would expect the architecture around network_client to be helpful here.

Let us know how it goes!

Thank you very much! Will study the file sharing example later today after work :slight_smile: