@@ -215,6 +215,7 @@ type ProvideKey func() (crypto.PrivKey, peer.ID)
215215type PublishOptions struct {
216216 ready RouterReady
217217 customKey ProvideKey
218+ local bool
218219}
219220
220221type PubOpt func (pub * PublishOptions ) error
@@ -307,7 +308,7 @@ func (t *Topic) Publish(ctx context.Context, data []byte, opts ...PubOpt) error
307308 }
308309 }
309310
310- return t .p .val .PushLocal (& Message {m , "" , t .p .host .ID (), nil })
311+ return t .p .val .PushLocal (& Message {m , "" , t .p .host .ID (), nil , pub . local })
311312}
312313
313314// WithReadiness returns a publishing option for only publishing when the router is ready.
@@ -319,6 +320,18 @@ func WithReadiness(ready RouterReady) PubOpt {
319320 }
320321}
321322
323+ // WithLocalPublication returns a publishing option to notify in-process subscribers only.
324+ // It prevents message publication to mesh peers.
325+ // Useful in edge cases where the msg needs to be only delivered to the in-process subscribers,
326+ // e.g. not to spam the network with outdated msgs.
327+ // Should not be used specifically for in-process pubsubing.
328+ func WithLocalPublication (local bool ) PubOpt {
329+ return func (pub * PublishOptions ) error {
330+ pub .local = local
331+ return nil
332+ }
333+ }
334+
322335// WithSecretKeyAndPeerId returns a publishing option for providing a custom private key and its corresponding peer ID
323336// This option is useful when we want to send messages from "virtual", never-connectable peers in the network
324337func WithSecretKeyAndPeerId (key crypto.PrivKey , pid peer.ID ) PubOpt {
0 commit comments