This project implements a Publish-Subscribe (Pub-Sub) messaging system using Python sockets. The system consists of three main components:
- Broker: Manages subscriptions and forwards published messages to subscribers.
- Publisher: Sends messages to a specific topic.
- Subscriber: Receives messages for topics it has subscribed to.
- Copy all the contents from this repository.
- Open a terminal and navigate to the folder containing
broker.py
. - Start the broker by running:
python broker.py
- The broker will listen on port 9000 for connections from publishers and subscribers.
- Open another terminal and navigate to the same folder.
- Start a publisher instance by running:
python publisher.py <PUBLISHER_ID>
- The publisher can send messages using the format:
<TOPIC> <MESSAGE>
- Open another terminal and navigate to the same folder.
- Start a subscriber instance by running:
python subscriber.py <SUBSCRIBER_ID>
- The subscriber can use the following commands:
subscribe <TOPIC> unsubscribe <TOPIC>
- Message Routing: The broker delivers published messages to all subscribed clients.
- Topic-Based Filtering: Messages are categorized by topics, allowing targeted delivery.
- Real-Time Communication: Messages are sent instantly between publishers and subscribers.
# Terminal 1 - Start the broker
python broker.py
# Terminal 2 - Start a subscriber
python subscriber.py sub1
> subscribe weather
# Terminal 3 - Start a publisher
python publisher.py pub1
> weather Rainy day ahead!
# Subscriber Output
>> DELIVER weather Rainy day ahead! pub1 2025-03-14 10:30:00
- Multiple subscribers and publishers can run simultaneously.
- The system is event-driven, meaning messages are forwarded as soon as they arrive.
- The broker ensures message delivery to all active subscribers of a given topic.
- Implement message persistence for disconnected subscribers.
- Add QoS levels for guaranteed message delivery.
- Support distributed brokers for scalability.