Hello from the other side
Report Bug
A simple chat application that covers Django authorization, web sockets and authentication.
Demo:
0Nv91Na7fV.mp4
- CRUD users
- Create rooms and chat in groups!
- Good looking UI
- Authenticated WebSockets to prevent unauthorized access
- Clone or download the repository
git clone https://github.com/rustyxlol/Django-ChatApp.git
cd chat-app-django
- Create and activate virtual environment
python -m venv venv
Windows: venv\Scripts\activate.bat
Linux: source venv/bin/activate
- Install required packages
python -m pip install -r requirements.txt
- Navigate to chat-app-django and run the server
python manage.py runserver
Note: make migrations if any error occurs
python manage.py makemigrations
python manage.py migrate
- Navigate to http://127.0.0.1:8000 on a browser of your choice.
- Users model
- Pages: Login, Signup, Home, Profile, (part of send message, more in next part)
- CRUD operations on profile(username, profile picture...)
- Channels Library
- Implementing basic functionality first then redoing it in async
- TODO: Redis can come during production, channel layer is set to
InMemoryChannelLayer
for development. - Converted basic functionality to async
- Added context processors for sidebar channels
- Two public channels exist which anyone should be able to use, authentication in the next part.
- Added messages model for permanent storage
- The simplest approach is to use basic authentication provided by Channels by scoping our consumer in websocket connect function like so.
Note: AuthMiddlewareStack is required.
user = self.scope['user']
if user.is_authenticated:
// authenticated user connection
else:
// unauthenticated acces - disconnect/close
-
Another approach is to use Tokens - essentially create a token on the client side and send it over to the backend for authentication.
- Requires custom middleware
- Requires
djangorestframework
- Hard to work with because tokens cannot be passed to headers
- Have to establish connection first, then send a token, might pose security risks
-
Third approach is to use sessions
The first and second approaches are covered in this application, look session-based
branch for the third approach.
- Django Tutorial - Corey Schafer
- Django Channels - RealPython
- Django Channels
- Django Channels and WebSockets oversimplified - Dennis Ivy
- Token Auth middleware ideas
Distributed under the MIT License. See LICENSE
for more information.