dart_libp2p_pubsub 1.0.1 copy "dart_libp2p_pubsub: ^1.0.1" to clipboard
dart_libp2p_pubsub: ^1.0.1 copied to clipboard

A comprehensive libp2p pubsub implementation for Dart, featuring GossipSub v1.1, FloodSub, and RandomSub protocols with message validation, peer scoring, and tracing support.

dart_libp2p_pubsub #

A comprehensive libp2p pubsub implementation for Dart, featuring GossipSub v1.1, FloodSub, and RandomSub protocols with message validation, peer scoring, and tracing support.

Pub Version Dart CI License

Features #

πŸš€ Multiple PubSub Protocols

  • GossipSub v1.1 - Production-ready, efficient pubsub protocol with mesh-based routing
  • FloodSub - Simple flooding protocol for development and testing
  • RandomSub - Randomized message propagation for research

πŸ”’ Security & Validation

  • Message validation with custom validators
  • Peer scoring system for network health
  • Cryptographic message signing and verification

πŸ“Š Monitoring & Debugging

  • Comprehensive event tracing
  • JSON and Protocol Buffer trace formats
  • Built-in logging and metrics

⚑ Performance

  • Efficient message caching with MCache
  • Optimized RPC queue management
  • Configurable mesh parameters for different use cases

Quick Start #

Installation #

Add to your pubspec.yaml:

dependencies:
  dart_libp2p_pubsub: ^1.0.1
  dart_libp2p: ^0.5.2

Basic Usage #

import 'package:dart_libp2p_pubsub/dart_libp2p_pubsub.dart';
import 'package:dart_libp2p/core/host/host.dart';

// Create a libp2p host
final host = await createLibp2pHost();

// Set up GossipSub router
final router = GossipSubRouter();
final pubsub = PubSub(host, router);

// Start the pubsub system
await pubsub.start();

// Subscribe to a topic
const topic = '/my-app/chat';
final subscription = pubsub.subscribe(topic);

// Listen for messages
subscription.stream.listen((message) {
  print('Received: ${String.fromCharCodes(message.data)}');
});

// Publish a message
final messageData = Uint8List.fromList('Hello, World!'.codeUnits);
await pubsub.publish(topic, messageData);

Examples #

Chat Application #

Run a simple peer-to-peer chat:

# Terminal 1
dart example/chat.dart

# Terminal 2 (connect to the first node)
dart example/chat.dart /ip4/127.0.0.1/tcp/4001/p2p/QmPeerId...

Custom Message Validation #

// Define a custom validator
bool validateChatMessage(String topic, dynamic message) {
  if (topic != '/chat/1.0.0') return false;
  
  final data = String.fromCharCodes(message.data);
  return data.length <= 1000; // Max 1000 characters
}

// Register the validator
pubsub.addValidator('/chat/1.0.0', validateChatMessage);

Peer Scoring #

// Configure peer scoring parameters
final scoreParams = PeerScoreParams(
  behaviourPenaltyWeight: -10.0,
  behaviourPenaltyDecay: 0.99,
  behaviourPenaltyThreshold: -100.0,
);

final pubsub = PubSub(host, router, scoreParams: scoreParams);

Documentation #

πŸ“š Comprehensive Guides

Architecture #

The library is organized into several key components:

lib/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ core/           # Core pubsub functionality
β”‚   β”‚   β”œβ”€β”€ pubsub.dart      # Main PubSub class
β”‚   β”‚   β”œβ”€β”€ message.dart     # Message handling
β”‚   β”‚   β”œβ”€β”€ subscription.dart # Topic subscriptions
β”‚   β”‚   └── validation.dart  # Message validation
β”‚   β”œβ”€β”€ gossipsub/      # GossipSub v1.1 implementation
β”‚   β”‚   β”œβ”€β”€ gossipsub.dart   # Main router
β”‚   β”‚   β”œβ”€β”€ mcache.dart      # Message cache
β”‚   β”‚   └── score.dart       # Peer scoring
β”‚   β”œβ”€β”€ floodsub/       # FloodSub protocol
β”‚   β”œβ”€β”€ randomsub/      # RandomSub protocol
β”‚   └── tracing/        # Event tracing

Contributing #

We welcome contributions! Please see our Contributing Guide for details.

Development Setup #

# Clone the repository
git clone https://github.com/stephanfeb/dart_libp2p_pubsub.git
cd dart_libp2p_pubsub

# Install dependencies
dart pub get

# Run tests
dart test

# Generate protobuf files
dart run build_runner build

License #

This project is licensed under the MIT License - see the LICENSE file for details.

Support #

0
likes
130
points
18
downloads

Publisher

unverified uploader

Weekly Downloads

A comprehensive libp2p pubsub implementation for Dart, featuring GossipSub v1.1, FloodSub, and RandomSub protocols with message validation, peer scoring, and tracing support.

Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

base32, bs58, clock, collection, cryptography, dart_libp2p, dart_libp2p_kad_dht, dart_multihash, dart_udx, dcid, fixnum, logging, protobuf, synchronized, uuid, x25519

More

Packages that depend on dart_libp2p_pubsub