|
| 1 | +GossipSub 1.3 Extensions and Topic Observation |
| 2 | +============================================== |
| 3 | + |
| 4 | +Overview |
| 5 | +-------- |
| 6 | + |
| 7 | +Py-libp2p supports the GossipSub v1.3 Extensions Control Message mechanism and the |
| 8 | +Topic Observation extension. These features require negotiating the |
| 9 | +``/meshsub/1.3.0`` protocol (or later) with peers. |
| 10 | + |
| 11 | +Topic Observation |
| 12 | +----------------- |
| 13 | + |
| 14 | +The Topic Observation extension allows a peer to receive IHAVE notifications for |
| 15 | +a topic without being a full subscriber. This is useful for presence awareness: |
| 16 | +knowing when messages are published on a topic without actually receiving the |
| 17 | +message payloads. |
| 18 | + |
| 19 | +Lifecycle |
| 20 | +~~~~~~~~~ |
| 21 | + |
| 22 | +1. **Start observing**: Call ``start_observing_topic(topic)`` to send OBSERVE |
| 23 | + control messages to in-topic peers that support the extension. The router |
| 24 | + will then send IHAVE notifications to you when new messages arrive on that |
| 25 | + topic. |
| 26 | + |
| 27 | +2. **Receive IHAVE**: As an observer, you receive IHAVE control messages |
| 28 | + containing message IDs. These are presence notifications only; observers do |
| 29 | + not typically reply with IWANT to fetch the actual messages. |
| 30 | + |
| 31 | +3. **Stop observing**: Call ``stop_observing_topic(topic)`` to send UNOBSERVE |
| 32 | + control messages and stop receiving IHAVE notifications for that topic. |
| 33 | + |
| 34 | +API Usage Snippet |
| 35 | +~~~~~~~~~~~~~~~~~ |
| 36 | + |
| 37 | +The snippet below demonstrates the Topic Observation API calls. It is not a |
| 38 | +complete runnable program (host setup, service lifecycle, and peer wiring are |
| 39 | +omitted for brevity). For a runnable end-to-end example, see |
| 40 | +:doc:`examples.pubsub`. |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | +
|
| 44 | + from libp2p import new_host |
| 45 | + from libp2p.pubsub.gossipsub import PROTOCOL_ID_V13, GossipSub |
| 46 | + from libp2p.pubsub.pubsub import Pubsub |
| 47 | +
|
| 48 | + # Create host and Pubsub with a v1.3-capable GossipSub router. |
| 49 | + host = new_host() |
| 50 | + gossipsub = GossipSub( |
| 51 | + protocols=[PROTOCOL_ID_V13], |
| 52 | + degree=6, |
| 53 | + degree_low=4, |
| 54 | + degree_high=12, |
| 55 | + ) |
| 56 | + pubsub = Pubsub(host, gossipsub) |
| 57 | +
|
| 58 | + # Start observing a topic (IHAVE-only presence notifications). |
| 59 | + # In practice, call this once Pubsub/GossipSub services are running. |
| 60 | + await gossipsub.start_observing_topic("my-topic") |
| 61 | +
|
| 62 | + # ... later, when done ... |
| 63 | + await gossipsub.stop_observing_topic("my-topic") |
| 64 | +
|
| 65 | +Protocol Requirements |
| 66 | +~~~~~~~~~~~~~~~~~~~~~ |
| 67 | + |
| 68 | +* Topic Observation requires both peers to negotiate ``/meshsub/1.3.0`` (or |
| 69 | + later) and to advertise support via the Extensions Control Message. |
| 70 | +* Extensions are only sent when the negotiated protocol is v1.3+; peers on |
| 71 | + v1.1/v1.2 do not receive extension fields. |
| 72 | + |
| 73 | +Specification References |
| 74 | +------------------------ |
| 75 | + |
| 76 | +* `GossipSub v1.3 Extensions <https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.3.md>`_ |
| 77 | +* `Topic Observation proposal <https://ethresear.ch/t/gossipsub-topic-observation-proposed-gossipsub-1-3/20907>`_ |
| 78 | + |
| 79 | +Related Documentation |
| 80 | +--------------------- |
| 81 | + |
| 82 | +* :doc:`gossipsub-1.2` - GossipSub 1.2 features (IDONTWANT, etc.) |
| 83 | +* :doc:`examples.pubsub` - PubSub chat example |
| 84 | +* :doc:`libp2p.pubsub` - Complete PubSub API documentation |
0 commit comments