Skip to main content

Command Palette

Search for a command to run...

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

Published
β€’4 min read
Building a Real-Time Chat App with PyQt5, WebSockets, and MongoDB πŸš€
K

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 πŸ”„

  1. User Registration: πŸ‘€ Users enter a username in a simple login dialog. Each username is assigned a unique color.

  2. Real-Time Messaging: πŸ’¬ Messages are sent using Socket.IO. The server broadcasts messages to everyone or only to a selected recipient for private messages.

  3. Typing Indicators: ⌨️ When a user types, the typing event is sent to other clients to show real-time typing notifications.

  4. Message History: πŸ“œ On connecting, the client fetches the last 100 messages from MongoDB Atlas and displays them with date separators.

  5. 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 PyInstaller required 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 πŸš€

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 πŸ“Š

LayerTechnologies
FrontendPyQt5
BackendPython, aiohttp, python-socketio
DatabaseMongoDB Atlas, pymongo, dnspython
DeploymentRender.com
PackagingPyInstaller (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! πŸ‘‡

GitHub

Download Release

Star the repo ⭐ if you find this project interesting!

Connect with me