Real-time Messaging and PubSub
While content addressing and storage handle data at rest, many P2P applications need real-time message dissemination: chat, live updates, collaborative editing signals, blockchain block propagation, and sensor data streams.
Gossip Protocols
Gossip (epidemic) protocols spread messages probabilistically, inspired by how rumors spread in social networks:
Each node receiving a new message forwards it to a random subset of peers (the "fanout").
With sufficient fanout, messages reach all n nodes in O(log n) rounds with high probability.
The fundamental trade-off: higher fanout means more reliable and faster delivery but more redundant messages (bandwidth overhead). Lower fanout saves bandwidth but risks partitions.
Eager vs lazy gossip: eager gossip sends the full message; lazy gossip sends only message IDs and lets peers request missing messages. GossipSub combines both.
Gossip protocols are inherently robust to node failures because there's no single point of failure and multiple paths carry each message.
GossipSub
GossipSub, developed for libp2p and used by Ethereum 2.0 consensus and Filecoin, is the state-of-the-art mesh-based PubSub protocol:
Mesh layer: for each topic, a sparse, connected overlay of D peers (typically D=6) provides full message delivery via eager push. Every message is forwarded to all mesh peers.
Gossip layer: periodic metadata heartbeats (IHAVE/IWANT) provide cache awareness and mesh repair. Peers that aren't in the mesh still learn about messages and can request ones they missed.
Peer scoring: nodes compute scores for each peer based on message delivery, timeliness, duplicates, invalid messages, and protocol compliance. Low-scoring peers are pruned from the mesh, defending against Sybil and eclipse attacks without needing a central identity authority.
Flood publishing: when you originate a message, send it to all connected topic peers (not just mesh peers) for rapid first-hop propagation.
Graft/Prune: adaptive mesh maintenance. GRAFT adds a peer to your mesh; PRUNE removes one. Backoff timers prevent thrashing.
Opportunistic grafting: periodically try to improve the mesh by grafting high-scoring non-mesh peers.
GossipSub v1.1 added significant attack resilience, making it suitable for consensus-critical messaging in Ethereum.
Topics and Discovery
Explicit topics: peers subscribe to named topic strings; the PubSub system forms per-topic meshes. Topics provide natural namespacing.
Content-interest filters: finer-grained subscriptions using Bloom filters or prefix matching on message attributes. Reduces bandwidth for peers who only care about a subset of topic traffic.
Rendezvous points: well-known nodes or DHT keys where topic subscribers register for discovery. libp2p has a dedicated rendezvous protocol for this.
Ambient discovery: in SSB, there are no explicit topics—you replicate feeds of people you follow, and content discovery is social rather than topical.
Ordering and Reliability
P2P messaging is at-most-once by default—the network makes best-effort delivery with no guarantees. Applications that need stronger guarantees layer on:
Application-layer acknowledgments: explicit acks/nacks for end-to-end reliability. The sender retransmits after timeout.
Causal ordering: vector clocks or Lamport timestamps ensure that if message A caused message B, A is delivered before B. Essential for CRDTs that require causal delivery of operations.
Total ordering: all peers see messages in the same global order. Much harder in P2P; typically achieved only via consensus protocols (Raft, PBFT, blockchain) or approximated via synchronized timestamps. Most P2P messaging avoids total ordering due to its cost.
Exactly-once semantics: achieved by combining at-least-once delivery with idempotent processing or deduplication. CRDTs are naturally idempotent, making them well-suited to P2P messaging.
PubSub in Practice
Ethereum consensus: GossipSub carries beacon block proposals, attestations, and sync committee messages across the libp2p P2P network.
Matrix: room events are distributed to participating servers. P2P Matrix experiments would use PubSub-like dissemination.
Nostr: events are published to relay servers that forward to subscribers—a centralized PubSub pattern with decentralized relay choice.
Collaborative editing: real-time CRDT operations are disseminated via PubSub for low-latency collaboration.
How PubSub Connects to Everything Else
PubSub uses Transport and Connectivity for peer connections and Overlay Networks for peer and topic discovery. It provides the dissemination layer for State Sync and CRDTs. Privacy considerations apply strongly—topic subscriptions reveal interest metadata to mesh peers. Peer scoring is a form of behavioral identity and defense against security threats. Testing PubSub requires measuring message delivery ratios and propagation latency under churn.
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime