A simple TCP socket-based publish/subscribe system where each client can act as:
- Subscriber (subscribe to topics and receive updates)
- Publisher (post messages to topics)
The server routes published messages to all subscribers of the selected topic.
- Multi-client support using threads
- Topic-based message distribution
- Shared topic definitions via
topics.py - Real-time notifications to subscribed clients
- Single client app that can both publish and subscribe
publish_subscribe_notification/
├── server.py # TCP server, subscription store, broadcast logic
├── client.py # Interactive publisher/subscriber client
├── topics.py # Allowed topics shared by server and client
└── README.md
- Python 3.8+
- No external dependencies (uses standard library only)
You can access the website at : https://netthreads.up.railway.app/
python server.pyThe server listens on:
- Host:
0.0.0.0 - Port:
5000
Open a new terminal for each client:
python client.pyBy default, the client connects to 127.0.0.1:5000.
If your server is on another machine, update SERVER_HOST in client.py.
Inside the client prompt:
topics→ list all available topicssubscribe <topic>→ subscribe to a topicunsubscribe <topic>→ request unsubscribe from a topicpost <topic> <message>→ publish a message to a topichelp→ show command helpquit→ close client
Example:
> subscribe sports
> post sports Messi scores in stoppage time!
The system uses a simple |-separated command protocol:
- Subscribe:
SUBSCRIBE|<topic> - Unsubscribe:
UNSUBSCRIBE|<topic> - Post:
POST|<topic>|<message>
Broadcast format from server to subscribers:
[TOPIC] message text
Defined in topics.py:
- sports
- world_news
- national_news
- pesu_live
- music
- gaming
- hollywood
- bollybuzz
- stocks
Open 4 terminals total:
- Terminal 1: server
- Terminal 2: Client A (subscriber)
- Terminal 3: Client B (subscriber)
- Terminal 4: Client C (publisher)
python server.py
Connected: ('127.0.0.1', 54321)
Connected: ('127.0.0.1', 54322)
Connected: ('127.0.0.1', 54323)
python client.py
> subscribe sports
[NOTIFICATION] Subscribed to sports
python client.py
> subscribe sports
[NOTIFICATION] Subscribed to sports
python client.py
> post sports Match starts at 7 PM today
Both Client A and Client B receive:
[NOTIFICATION] [SPORTS] Match starts at 7 PM today
This confirms the publish/subscribe flow is working.
- The client sends
UNSUBSCRIBE, but the server must implement handling for this command to fully support unsubscribe behavior. - Current server message parsing is minimal and assumes valid command format.
- No authentication or encryption (for learning/demo use).
- Add
UNSUBSCRIBEhandling in server logic - Prevent duplicate subscriptions per client/topic
- Improve input validation and error responses
- Add graceful shutdown and cleanup of disconnected sockets
- Add logging and unit tests
This project is for educational use.