Skip to main content

Real-time one-on-one chat system for Django projects, powered by WebSocket and JWT authentication.

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

Django Chelseru Chat

A simple real-time chat package for Django using Django Channels and WebSocket. It enables one-on-one private messaging secured with JWT authentication.


Installation

pip install django-chelseru-chat

Configuration

Ensure your Django project is ASGI-compatible and set up with JWT authentication for WebSocket connections.

1. Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'channels',
    'chat',
]

2. Set ASGI_APPLICATION in settings.py:

ASGI_APPLICATION = '<your_project_name>.asgi.application'

Replace <your_project_name> with the actual name of your Django project folder (e.g., myproject).

3. Use your custom asgi.py with JWTAuthMiddleware:

# <your_project_name>/asgi.py

import os
import django
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

# Set environment and initialize Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your_project_name>.settings')
django.setup()

# Import routing and middleware AFTER setup
import chat.routing
from chat.middleware import JWTAuthMiddleware

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": JWTAuthMiddleware(
        URLRouter(
            chat.routing.websocket_urlpatterns
        )
    ),
})

Again, replace <your_project_name> with your Django project's name.


Models

ChatRoom

class ChatRoom(models.Model):
    user_1 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user1_chats')
    user_2 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user2_chats')
    created_at = models.DateTimeField(auto_now_add=True)

Message

class Message(models.Model):
    chat_room = models.ForeignKey(ChatRoom, on_delete=models.CASCADE, related_name='messages')
    sender = models.ForeignKey(User, on_delete=models.CASCADE)
    text = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)

WebSocket Connection

To send and receive messages, connect via WebSocket to:

ws://<your-domain>/ws/chat/<chat_room_id>/?token=<your_jwt_access_token>

Example:

ws://qesa.chelseru.com:8000/ws/chat/3/?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

To send a message:

> {"message": "hi"}

You will receive a response like:

< {"message": "hi", "sender": "user"}

Create ChatRoom Programmatically

from chat.models import ChatRoom
chat = ChatRoom.objects.create(user_1=user1, user_2=user2)

JWT WebSocket Authentication

This package uses a custom JWTAuthMiddleware for authenticating users via JWT tokens.
Token must be provided as a query parameter: ?token=...

You can use djangorestframework-simplejwt to issue tokens.


Features

  • Private 1-on-1 chat
  • Real-time messaging via WebSocket
  • JWT-authenticated WebSocket
  • Message history per room
  • Simple model structure

TODO

  • Group chat support
  • Message read status
  • Typing indicators

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_chelseru_chat-1.2.2.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_chelseru_chat-1.2.2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file django_chelseru_chat-1.2.2.tar.gz.

File metadata

  • Download URL: django_chelseru_chat-1.2.2.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for django_chelseru_chat-1.2.2.tar.gz
Algorithm Hash digest
SHA256 8d325816d454e1a8e684c16d5d6c30b7650e0159f6cf94a7ab8c7da50b3e83da
MD5 66a3a3dc5610cfea77ee48b4d8f45bac
BLAKE2b-256 85ba9c3920ef095a057e16f2f3bebef16da5942852e208c7d12640dc366b4fa6

See more details on using hashes here.

File details

Details for the file django_chelseru_chat-1.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_chelseru_chat-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8969e4003d5de0d0dc0172dad9b7added712c30c5c6401508c6407e01fe55b6a
MD5 46a9c5b2935e55f1babdbbbf7e5a189f
BLAKE2b-256 d82dd97b86c1ee718210cf90f5cfd2f59578c8802cc42bb9d7e3f1303228bd14

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page