mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
* Initial setup for pubsup * Created node and trying to setup gossipsub * Fix: Use pubsub object for publishing messages instead of gossipsub * Correct help message for port argument. * Fix: Used pubsub object instead of gossipsub object on Client side * Fix: handle_new_peer method of pubsub is used to connect to new peers. * used for host.connect to connect to peers * Corrected script for connecting to other peers. * message receiving function created * message publishing function created * Refactored the code for improved clarity and maintainability. * fix: make publish loop input non-blocking to prevent event loop blocking * refactored the code for better user experience while publishing message * corrected the name of protocol * Fix: Correct the implementation of the port argument * Added pubsub initialization * added logging * pubsub instance is running * Enhance publish loop with user prompts and error handling * Connection monitoring added * Add key pair generation and security options to pubsub host initialization * Refactor pubsub logging and corrected gossipsub protocol id * Started gossipsub service * Add dynamic port assignment * Refactor pubsub example for CI * feat: monitor_peer_topics function added * Noise protocol added * refactor: default port set to none and some logging changes. * refactor: Add graceful shutdown with termination events - Replace infinite loops with termination events - Add proper shutdown handling for all loops - Implement clean resource cleanup on exit - Add shutdown message for better user feedback - Update signal handling for graceful termination * Changed import path for factories file. - to align import statement with changes from PR 543 * Added News Fragment * Added pub-sub demo to the console_scripts section in setup.py * Added pubsub example to Documentation * Fix formatting and path in PubSub documentation example * Added pubsub example in toctree * Added tests for pubsub example * updated the description of pubsub example * corrected the name of pubsub docs file * Remove unused imports and security options from pubsub example * Update script usage instructions in pubsub example * Enhanced compatibility for python 3.9 * Corrected console output
65 lines
3.6 KiB
ReStructuredText
65 lines
3.6 KiB
ReStructuredText
|
|
PubSub Chat Demo
|
|
================
|
|
|
|
This example demonstrates how to create a chat application using libp2p's PubSub implementation with the GossipSub protocol.
|
|
|
|
.. code-block:: console
|
|
|
|
$ python -m pip install libp2p
|
|
Collecting libp2p
|
|
...
|
|
Successfully installed libp2p-x.x.x
|
|
$ pubsub-demo
|
|
2025-04-06 23:59:17,471 - pubsub-demo - INFO - Running pubsub chat example...
|
|
2025-04-06 23:59:17,471 - pubsub-demo - INFO - Your selected topic is: pubsub-chat
|
|
2025-04-06 23:59:17,472 - pubsub-demo - INFO - Using random available port: 33269
|
|
2025-04-06 23:59:17,490 - pubsub-demo - INFO - Node started with peer ID: QmcJnocH1d1tz3Zp4MotVDjNfNFawXHw2dpB9tMYGTXJp7
|
|
2025-04-06 23:59:17,490 - pubsub-demo - INFO - Listening on: /ip4/0.0.0.0/tcp/33269
|
|
2025-04-06 23:59:17,490 - pubsub-demo - INFO - Initializing PubSub and GossipSub...
|
|
2025-04-06 23:59:17,491 - pubsub-demo - INFO - Pubsub and GossipSub services started.
|
|
2025-04-06 23:59:17,491 - pubsub-demo - INFO - Pubsub ready.
|
|
2025-04-06 23:59:17,491 - pubsub-demo - INFO - Subscribed to topic: pubsub-chat
|
|
2025-04-06 23:59:17,491 - pubsub-demo - INFO - Run this script in another console with:
|
|
pubsub-demo -d /ip4/127.0.0.1/tcp/33269/p2p/QmcJnocH1d1tz3Zp4MotVDjNfNFawXHw2dpB9tMYGTXJp7
|
|
|
|
2025-04-06 23:59:17,491 - pubsub-demo - INFO - Waiting for peers...
|
|
Type messages to send (press Enter to send):
|
|
|
|
Copy the line that starts with ``pubsub-demo -d``, open a new terminal and paste it in:
|
|
|
|
.. code-block:: console
|
|
|
|
$ pubsub-demo -d /ip4/127.0.0.1/tcp/33269/p2p/QmcJnocH1d1tz3Zp4MotVDjNfNFawXHw2dpB9tMYGTXJp7
|
|
2025-04-07 00:00:59,845 - pubsub-demo - INFO - Running pubsub chat example...
|
|
2025-04-07 00:00:59,846 - pubsub-demo - INFO - Your selected topic is: pubsub-chat
|
|
2025-04-07 00:00:59,846 - pubsub-demo - INFO - Using random available port: 51977
|
|
2025-04-07 00:00:59,864 - pubsub-demo - INFO - Node started with peer ID: QmYQKCm95Ut1aXsjHmWVYqdaVbno1eKTYC8KbEVjqUaKaQ
|
|
2025-04-07 00:00:59,864 - pubsub-demo - INFO - Listening on: /ip4/0.0.0.0/tcp/51977
|
|
2025-04-07 00:00:59,864 - pubsub-demo - INFO - Initializing PubSub and GossipSub...
|
|
2025-04-07 00:00:59,864 - pubsub-demo - INFO - Pubsub and GossipSub services started.
|
|
2025-04-07 00:00:59,865 - pubsub-demo - INFO - Pubsub ready.
|
|
2025-04-07 00:00:59,865 - pubsub-demo - INFO - Subscribed to topic: pubsub-chat
|
|
2025-04-07 00:00:59,866 - pubsub-demo - INFO - Connecting to peer: QmcJnocH1d1tz3Zp4MotVDjNfNFawXHw2dpB9tMYGTXJp7 using protocols: MultiAddrKeys(<Multiaddr /ip4/127.0.0.1/tcp/33269/p2p/QmcJnocH1d1tz3Zp4MotVDjNfNFawXHw2dpB9tMYGTXJp7>)
|
|
2025-04-07 00:00:59,866 - pubsub-demo - INFO - Run this script in another console with:
|
|
pubsub-demo -d /ip4/127.0.0.1/tcp/51977/p2p/QmYQKCm95Ut1aXsjHmWVYqdaVbno1eKTYC8KbEVjqUaKaQ
|
|
|
|
2025-04-07 00:00:59,881 - pubsub-demo - INFO - Connected to peer: QmcJnocH1d1tz3Zp4MotVDjNfNFawXHw2dpB9tMYGTXJp7
|
|
Type messages to send (press Enter to send):
|
|
|
|
You can then start typing messages in either terminal and see them relayed to the other terminal. The messages will be distributed using the GossipSub protocol to all peers subscribed to the same topic. To exit the demo, type "quit" or send a keyboard interrupt (``Ctrl+C``) in either terminal.
|
|
|
|
Command Line Options
|
|
--------------------
|
|
|
|
- ``-t, --topic``: Specify the topic name to subscribe to (default: "pubsub-chat")
|
|
- ``-d, --destination``: Address of peer to connect to
|
|
- ``-p, --port``: Port to listen on (default: random available port)
|
|
- ``-v, --verbose``: Enable debug logging
|
|
|
|
The full source code for this example is below:
|
|
|
|
.. literalinclude:: ../examples/pubsub/pubsub.py
|
|
:language: python
|
|
:linenos:
|