This is a simple chat application that allows users to chat with each other in real time. It is built using python, websockets, and MongoDB.
sudo apt install docker.io
sudo apt install docker-compose
docker-compose up
pip install -r requirements.txt
python http_server.py
python websocket_server.py
- handle all the data via nosql database (MongoDB)
- leverage the power of docker and docker-compose
- custom http server to handle concurrent requests
- custom websocket server to handle concurrent requests
- unit tests
- serve static files
- User Authentication
- User Registration
- User Login
- User could create a room
- User could join a room
- realtime room chat
- realtime typing effect
- realtime online users
This Python script implements a WebSocket chat server with user authentication, message broadcasting, and MongoDB integration.
asyncio
: Asynchronous I/O for handling multiple WebSocket connections concurrently.json
: For encoding and decoding JSON data.websockets
: Library for WebSocket communication.os
: For environment variable and directory path handling.dotenv
: Loads environment variables from a.env
file.models.Message
: Represents a chat message and is used for creating and storing messages in MongoDB.mongodb_operations.MongoDBOperations
: Handles MongoDB operations for user authentication and message storage.
HOST
: WebSocket server host address.PORT
: WebSocket server port.STATIC_DIR
: Directory path for static files.
MONGO_URI
: MongoDB connection URI.DB_NAME
: MongoDB database name.
- Ensure all required dependencies are installed (
asyncio
,json
,websockets
,os
,dotenv
,models
,mongodb_operations
). - Create a
.env
file with the necessary configuration variables. - Run the script (
python script_name.py
).
The server will start listening for WebSocket connections, handle authentication, and broadcast messages to connected users in specific rooms. The server also integrates with MongoDB to store chat messages.
Handles a WebSocket connection and manages user authentication, message broadcasting, and database storage.
-
Parameters:
websocket
: WebSocket connection object.path
: WebSocket connection path.
-
Raises:
websockets.exceptions.ConnectionClosedError
: If the connection is closed by the client.
Broadcasts a message to all connected users in a specific room.
- Parameters:
message
: Message to be broadcasted.connected_users
: List of connected users in a room.
Main function to run the WebSocket server.
This function creates and serves the WebSocket server, handling incoming connections.
- Usage:
asyncio.run(main())
python script_name.py
- Uses the
hashlib
library to hash passwords using SHA-256 with a predefined salt.
- Manages interactions with a MongoDB database.
- Provides methods for creating, authenticating, and retrieving users, rooms, and messages.
- Handles basic error logging for user creation.
- Demonstrates the usage of the
MongoDBOperations
class. - Creates a user, authenticates the user, creates a room, and creates a message.
- Retrieves user, room, and message information from the database.
- Instantiates the
MongoDBOperations
class with MongoDB URI and database name.
- Calls the
run
function to showcase the functionality.
Note: Ensure a local MongoDB server is running. The code contains basic error handling for user creation and prints error messages to the console.