- Overview
- Learning Objectives
- System Architecture
- Prerequisites
- Project Structure
- Implementation Tasks
- How to Run
- Concepts Explained
This project implements a Publish-Subscribe (Pub/Sub) Middleware using socket programming in Python. It demonstrates asynchronous communication patterns where:
- Publishers send messages on specific topics
- Subscribers receive messages from topics they're interested in
- Middleware (Server) routes messages between publishers and subscribers
- Chat Applications: WhatsApp, Slack, Discord
- IoT Systems: Sensor data distribution
- Stock Market: Real-time price updates
- Notification Systems: Push notifications
- Distributed Systems: Microservices communication
-
Socket Programming Fundamentals
- TCP/IP communication
- Client-server architecture
- Connection management
-
Concurrent Programming
- Multi-threading concepts
- Thread synchronization
- Race condition handling
-
Design Patterns
- Publish-Subscribe pattern
- Observer pattern
- Message routing
-
Distributed Systems Concepts
- Message queuing
- Topic-based filtering
- System reliability & availability
ββββββββββββββ ββββββββββββββ
β Client β βββββββ>β Server β
β β<βββββββ β β
ββββββββββββββ ββββββββββββββ
βββββββββββββββ ββββββββββββββ βββββββββββββββ
β Publisher 1 β βββββ> β β βββββ> β Subscriber 1β
βββββββββββββββ β Server β βββββββββββββββ
βββββββββββββββ β(Middleware)β βββββββββββββββ
β Publisher 2 β βββββ> β β βββββ> β Subscriber 2β
βββββββββββββββ ββββββββββββββ βββββββββββββββ
Publisher (SPORTS) βββββββ
βββ> Server ββ> Subscriber (SPORTS)
Publisher (NEWS) βββββββββ€ βββ> Subscriber (NEWS)
βββββββββββββββ> Subscriber (SPORTS)
Goal: Establish basic socket connection and message exchange
Key Concepts:
- Socket creation (
socket.socket()) - Binding server to address (
bind()) - Listening for connections (
listen()) - Accepting connections (
accept()) - Sending/receiving data (
send(),recv())
Expected Output:
- Server starts and listens on port 5000
- Client connects to server
- Text typed in client appears on server terminal
- Typing "terminate" closes client connection
Goal: Handle multiple concurrent clients with different roles
Key Concepts:
- Threading for concurrent connections (
threading.Thread) - Client role identification (PUBLISHER vs SUBSCRIBER)
- Message broadcasting to specific client types
- Thread-safe data structures (
Lock,Queue)
Expected Output:
- Multiple clients connect simultaneously
- Publisher clients send messages
- Only Subscriber clients receive published messages
- Server displays all client activities
Goal: Route messages based on topics/subjects
Key Concepts:
- Topic registration and management
- Selective message routing
- Data structure for topic-subscriber mapping
- Message filtering logic
Expected Output:
- Clients specify topics (e.g., SPORTS, NEWS, WEATHER)
- Publishers send messages on specific topics
- Only subscribers to that topic receive messages
- Multiple topics can coexist
Goal: Propose improved architecture for reliability
Key Concepts:
- Single Point of Failure (SPOF)
- Load balancing
- Server replication
- Message queuing
- Fault tolerance
Deliverable: Architecture diagram + written explanation
Terminal 1 (Server):
cd task1
python server.py 5000Terminal 2 (Client):
cd task1
python client.py localhost 5000(Detailed instructions for each task in respective README files)
A socket is an endpoint for sending or receiving data across a network. Think of it as a "phone line" between two programs.
Analogy:
- Server = Phone company's switchboard
- Client = Your phone
- Port = Phone line number
- Message = Conversation
- TCP (Transmission Control Protocol): Reliable, ordered, connection-based β (We use this)
- UDP (User Datagram Protocol): Fast, connectionless, no guarantee β
A single-threaded server can only handle one client at a time. Threading allows:
- Concurrent connections: Multiple clients simultaneously
- Non-blocking: Server doesn't freeze waiting for one client
- Scalability: Can handle many publishers/subscribers
Traditional Request-Response:
Client β "Give me data" β Server
Client β "Here's data" β Server
Pub/Sub:
Publisher β "Here's new data!" β Server β Interested Subscribers
(Publishers don't know who receives)
(Subscribers don't know who published)
Benefits:
- Decoupling: Publishers and subscribers are independent
- Scalability: Easy to add more publishers/subscribers
- Flexibility: Dynamic subscription management