How do you use WithReadiness with GossipSub?

I’m posting messages to a GossipSub topic, and I’d like to make sure the message gets to at least one other peer.

I see that there is a WithReadiness option I can use when publishing. However, when I use it the Publish function blocks forever, even though a peer is subscribed to the topic.

I see in the docs:

// WithReadiness returns a publishing option for only publishing when the router is ready.
// This option is not useful unless PubSub is also using WithDiscovery
func WithReadiness(ready RouterReady) PubOpt {

So it seems like maybe I need to use WithDiscovery on the GossipSub when I create it, but I’m not sure how to construct the discovery. Is there any examples for how to do this, or anyone able to point me in the right direction?

You dont need to use WithDiscovery any more.
Publishing will block until the router has enough peers to make a mesh; 1 is not enough.

Got it, thanks. This must explain why when I use WithReadiness with a gossipsub topic, it is blocking forever, because I only have one peer connected to the application publishing.

How do I in the code determine that a mesh has been formed, or two many peers are required?

You could ListPeers, polling.

In general the recommended way is WithReadiness, there is no simple way to expose mesh status (which could change at any time).

Nonetheless, you can also attach a (raw) tracer and process the Graft/Prune events, which allows you to reconstruct mesh state.