dart_libp2p_pubsub 1.0.1
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.
PubSub Chat Example #
This directory contains a simple command-line chat application that demonstrates the core features of the dart-libp2p-pubsub library.
Features Demonstrated #
- Creating a libp2p Host.
- Initializing the
PubSubservice withGossipSubRouter. - Connecting to a peer using a
MultiAddr. - Subscribing to a topic.
- Publishing messages from standard input.
- Receiving and displaying messages from the network.
- Graceful shutdown.
How to Run #
Prerequisites #
You need to have the Dart SDK installed.
The example code in chat.dart relies on a helper function, createLibp2pNode, which is fully implemented in the project's test suite at test/real_net_stack.dart. To make this example runnable, you would need to:
- Copy
test/real_net_stack.dartinto theexample/directory. - Update
example/chat.dartto import it:import 'real_net_stack.dart';. - Replace the dummy
createLibp2pNodefunction at the bottom ofchat.dartwith the actual implementation.
Running the Chat #
After setting up the createLibp2pNode function, you can run the chat application.
-
Open two terminal windows.
-
In the first terminal, start the first chat node:
dart run example/chat.dartThe application will start and print its listen address, which includes its unique PeerId. It will look something like this:
INFO: 2023-10-27 10:30:00: ChatExample: Listen addresses: [/ip4/127.0.0.1/udp/54321/udx/p2p/12D3KooW...somePeerId] -
Copy the full listen address from the first terminal.
-
In the second terminal, start the second node and pass the first node's address as an argument:
dart run example/chat.dart <paste_address_from_first_terminal_here>For example:
dart run example/chat.dart /ip4/127.0.0.1/udp/54321/udx/p2p/12D3KooW...somePeerId -
The two nodes will connect. You can now type a message in either terminal and press
Enterto send it. The message will appear on the other node's screen. -
To exit, press
Ctrl+Cin either terminal.