This is a simple real-time chat application built using Django and Django Channels. It allows multiple users to join a chat room and send/receive messages in real-time via WebSockets.
- User registration and login.
- Real-time messaging using Django Channels and WebSockets.
- Chat rooms where users can send messages to all connected participants.
- Automatically logs in the user after registration.
- Python 3.10 or higher
- Django
- Django Channels
- Daphne (ASGI server)
git clone https://github.com/binaryash/django-chat-app.git
cd django-chat-app
It is recommended to use a virtual environment for the project dependencies.
python -m venv venv
source venv/bin/activate # For Windows, use 'venv\Scripts\activate'
Install the required Python packages listed in requirements.txt
.
pip install -r requirements.txt
The requirements.txt
file includes the following:
Django
channels
(for WebSocket support)daphne
(ASGI server)
In your settings.py
, ensure you have the following configurations:
- Add
channels
to theINSTALLED_APPS
:
INSTALLED_APPS = [
# other apps
'channels',
]
- Set the
ASGI
application:
ASGI_APPLICATION = "your_project_name.asgi.application"
Daphne is used as the ASGI server to handle WebSocket connections.
- Install Daphne if you haven't already (it should be in your
requirements.txt
):
pip install daphne
- In your
asgi.py
file, ensure it's configured correctly to handle WebSockets:
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project_name.settings')
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
# Your app's routing
)
),
})
Run the following command to set up the database:
python manage.py migrate
Start the Daphne server to handle WebSocket connections:
daphne your_project_name.asgi:application
Alternatively, you can use python manage.py runserver
during development, but Daphne is recommended for WebSocket handling.
- Navigate to the registration page at
/register/
to create an account. - After logging in, users will be redirected to the chat room.
Django Channels enables WebSocket support in this chat application.
- ChatConsumer: A WebSocket consumer that handles user connections, messages, and disconnections. It uses Django's Channels layer to broadcast messages to all users in the chat room.
- A user sends a message via WebSocket.
- The message is broadcast to the group using
group_send
. - All users in the group receive the message in real-time via the WebSocket connection.
django-chat-app/
├── chat/
│ ├── consumers.py
│ ├── forms.py
│ ├── models.py
│ ├── urls.py
│ ├── views.py
│ └── templates/
│ └── chat/
│ ├── chatPage.html
│ └── registerPage.html
├── your_project_name/
│ ├── __init__.py
│ ├── asgi.py
│ └── settings.py
└── manage.py
This project is open-source and available under the MIT License.