Send message to already connected peer (context: IPFS)

There are a couple options you could use for doing this, depending on what you’re trying to achieve. Leveraging pubsub would be a good option if you want to broadcast messages to your network.

If pubsub doesn’t work for what you’re trying to do, then you can use ipfs.libp2p.dialProtocol as you’ve indicated. It will use the existing connection if there is one and just create a new stream. ipfs.swarm.peers will give you a list of your connected peers, so you could iterate over those for your dial.

1 Like