Building a Real-Time Chat App with PyQt5, WebSockets, and MongoDB π

Passionate developer with a keen interest in solving real-world problems using efficient algorithms. I write about Java, data structures, and algorithmic challenges, focusing on performance and simplicity. Currently exploring cloud computing, DevOps, and building web apps with React.js. On a journey to help others with tips and insights from my coding adventures. Letβs code and grow together!
Building desktop applications that communicate in real-time is always exciting! π¬ Recently, I completed a project where I created a cross-platform chat application using PyQt5 for the GUI, Socket.IO for real-time communication, and MongoDB Atlas for message storage. I also deployed the server on Render.com and created Linux and Windows executables for easy distribution. Here's my journey and the technologies I used. π¨βπ»
Overview of the Project β¨
The application allows multiple users to chat in real-time, including:
Public messages: Everyone sees these π
Private messages: Users can select a recipient from the user list π
Typing indicators: Shows when someone is typing β¨οΈ
Message history: Loads the last 100 messages from the database π
Color-coded usernames: Each user gets a unique color π¨
The project combines desktop GUI development, backend development, and real-time networking

Technologies Used π οΈ
Here's the stack I used for this project:
Frontend/Desktop:
PyQt5β Python library for building native desktop applications with GUI π₯οΈ
Backend/Server:
Python+aiohttpβ To run the async web server πpython-socketio[aiohttp]β For real-time communication between clients and server β‘MongoDB Atlasβ Cloud database to store chat messages βοΈ
Additional Python libraries:
dnspythonβ Required for MongoDB connection string parsing πpymongoβ For interacting with MongoDB ποΈ
Deployment:
Render.comβ For hosting the async Python server in the cloud π
Packaging:
- PyInstaller - Created executables for Windows and Linux so users can run the app without Python installed π¦
How the App Works π
User Registration: π€ Users enter a username in a simple login dialog. Each username is assigned a unique color.
Real-Time Messaging: π¬ Messages are sent using Socket.IO. The server broadcasts messages to everyone or only to a selected recipient for private messages.
Typing Indicators: β¨οΈ When a user types, the
typingevent is sent to other clients to show real-time typing notifications.Message History: π On connecting, the client fetches the last 100 messages from MongoDB Atlas and displays them with date separators.
User List and Colors: π¨ The server maintains a user list and unique color mapping and sends updates whenever users connect or disconnect.
Challenges & Learnings π§
Threading with PyQt5: π Updating the GUI from socket callbacks required signals and slots to avoid threading issues.
Real-time performance: β‘ Handling typing indicators and private messages without lag was tricky but fun! π
MongoDB Date Handling: π Storing timestamps in UTC and formatting them on the client side ensured correct display across time zones.
Cross-platform builds: π₯οΈ Packaging PyQt5 apps for Linux and Windows using
PyInstallerrequired careful testing of dependencies.
Project Structure π
Based on my actual project structure:
PyQt_Chat_with_MongoDB/
βββ client.py # Main client application
βββ server.py # Socket.IO server with MongoDB
βββ message_bubble.py # Custom message widget
βββ chat.ui # Qt Designer UI file
βββ requirements.txt # Python dependencies
βββ client.spec # PyInstaller spec file
βββ README.md # Project documentation
βββ build/ # PyInstaller build directory
βββ dist/ # Output executables
βββ __pycache__/ # Python cache files
βββ venv/ # Virtual environment
client.pyβ Main PyQt5 GUI application π₯οΈserver.pyβ Async Socket.IO server with MongoDB integration πmessage_bubble.pyβ Custom widget for chat bubbles π¬chat.uiβ Qt Designer interface file π¨requirements.txtβ All required Python libraries πclient.specβ PyInstaller configuration for packaging π¦
Deployment & Release π
The server is deployed on Render.com: https://pyqt-chat-with-mongodb.onrender.com βοΈ
The client executables for Linux and Windows are available in the v1.0 Pre-release: PyQt Chat v1.0 Release π¦
π₯ Download Links:
Linux: PyQtChat_Linux (75 MB)
Windows: PyQtChat_Windows.exe (40.7 MB)
Users can download and start chatting without installing Python π
Key Takeaways π‘
Real-time apps are easier with WebSockets than polling β‘
PyQt5 is robust for building professional desktop applications π₯οΈ
MongoDB Atlas makes cloud database integration seamless βοΈ
Signals and slots are essential when updating the GUI from background threads π
Packaging and cross-platform distribution are as important as coding the app itself π¦
Technologies Summary π
| Layer | Technologies |
| Frontend | PyQt5 |
| Backend | Python, aiohttp, python-socketio |
| Database | MongoDB Atlas, pymongo, dnspython |
| Deployment | Render.com |
| Packaging | PyInstaller (Windows/Linux) |
Final Thoughts π€
This project was a great way to learn and apply WebSockets, PyQt5 GUI development, and cloud database integration!
The application is now available as v1.0 pre-release with standalone executables for both Linux and Windows, making it accessible to users π
π‘ Future Enhancements: You can extend this app with file sharing π, emoji support π, or message reactions β€οΈ in the future!
π Happy coding! Feel free to check out the repository and contribute! π
Star the repo β if you find this project interesting!