This repository contains a simple chat application built with Django Channels. Django Channels extends Django to handle WebSockets, allowing real-time communication between clients and the server.
- Real-time chat using WebSockets
- User authentication and authorization
- Multiple chat rooms
- Message history
- Python 3.x
- Django
- Channels
- Redis (for handling WebSocket connections)
-
Clone the repository:
git clone https://github.com/yourusername/chat-application.git
-
Navigate to the project directory:
cd chat-application
-
Install dependencies:
pip install -r requirements.txt
-
Make migrations and apply them:
python manage.py makemigrations python manage.py migrate
-
Start the Django development server:
python manage.py runserver
-
Open your browser and go to
http://127.0.0.1:8000
to access the chat application.
Django Channels requires a channel layer to handle WebSocket connections. By default, this application is configured to use Redis as the channel layer. Make sure you have Redis installed and running.
Configure the channel layer in your Django settings:
# settings.py
INSTALLED_APPS = [
# ...
'channels',
]
# Use channels layer as the default backend for Django
ASGI_APPLICATION = 'your_project.routing.application'
# Use Redis as the channel layer
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
Create a routing.py
file in your project directory:
# routing.py
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
application = ProtocolTypeRouter(
{
'websocket': AuthMiddlewareStack(
URLRouter(
# your routing configuration here
)
),
}
)
Replace the comment in routing.py
with the actual routing configuration for your chat application.
- Create a user account or log in.
- Join an existing chat room or create a new one.
- Start chatting in real-time!
Feel free to explore the codebase and customize the application to fit your specific requirements. If you have any questions or issues, please open an issue.
Happy chatting!