How to check if a stream is already open with peer

Hey, I was wondering how to check if a stream with a certain protocol is already open with a peer.

I’ve been trying to open streams to peers but getting stream reset errors, so I’m guessing they have already opened a stream with me. Let me know if there is perhaps some other issue causing stream reset.

Thanks!

Hey @noot, what implementation of libp2p is this? go-libp2p? One way of doing this is: host.Network().ConnsToPeer(id). For each connection, iterate through streams via conn.GetStreams() and call the Protocol() getter on the stream.

Yep go-libp2p. That works for me, thank you!

I was wondering, what will cause a stream reset when trying to read from a stream? Is it if the other side closes/reopens the stream?

@noot do you mind opening another discussion for that? It’ll be useful for future visitors to headline that answer separately.

1 Like

I’ve opened another thread here What causes a "stream reset"? :slight_smile:

Hi @raul - thanks for this reply. I actually tried what you said but stumbled across this issue - the type of the stream object don’t match between a stream created using host.NewStream and what is found using host.Network().ConnsToPeer(id) and then conn.GetStreams().
NewStream returns - *basichost.streamWrapper while latter returns *swarm.Stream.
Is that as expected and can they be used interchangeably?
thanks

@vishal hey! Both of them return structs that conform to the network.Stream interface. See godocs:

You shouldn’t be needed to access the actual implementation type. Do you have a code sample you can post? Or maybe we can pair on this? (ping me via email)

What are you trying to achieve exactly?

1 Like