Skip to main content

Real-time WebSocket bridge for Django (using Channels)

Project description

🧩 djwsbridge

djwsbridge is a Django Channels-based library for real-time messaging via WebSocket.
It enables seamless integration for sending, storing, and broadcasting messages in real-time between users.


✨ Key Features

  • Real-time messaging between users over WebSocket.
  • Individual user channels (e.g., user_{id}) for private communication.
  • Message storage via a customizable Message model.
  • Chat context management through a customizable Chat model.
  • Fully configurable through Django's settings.py.
  • Ability to send messages from the backend using send_ws_message.

🚀 Installation

pip install djwsbridge

Or install locally (for development):

git clone https://github.com/username/djwsbridge.git
cd djwsbridge
pip install -e .

⚙️ Django Configuration

Add the following to your settings.py:

INSTALLED_APPS = [
    ...
    "daphne",
    "channels",
    "djwsbridge",
]

ASGI_APPLICATION = "your_project.asgi.application"

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
        },
    },
}

# Customizable settings:

# Path to your Chat model (format: "app_label.ModelName").
DJWSBRIDGE_CHAT_MODEL = "yourapp.Chat"

# Path to your Message model.
DJWSBRIDGE_MESSAGE_MODEL = "yourapp.Message"

# Allowed message types (optional).
DJWSBRIDGE_ALLOWED_TYPES = ["text", "message", "image", "signals"]

# WebSocket URL prefix (protocol).
DJWSBRIDGE_WS_PREFIX = "socket"

# WebSocket URL path.
DJWSBRIDGE_WS_PATH = "mychannel"

🔌 ASGI Configuration (asgi.py)

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

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
django.setup()

from djwsbridge.routing import get_websocket_urlpatterns
from djwsbridge.middleware import TokenAuthMiddleware

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": TokenAuthMiddleware(
        URLRouter(
            get_websocket_urlpatterns()
        )
    ),
})

🧑‍💻 Example Models (models.py)

from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()

class Chat(models.Model):
    participants = models.ManyToManyField(User, related_name="chats")
    created = models.DateTimeField(auto_now_add=True)

class Message(models.Model):
    chat = models.ForeignKey(Chat, on_delete=models.CASCADE)
    sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="sent_messages")
    recipient = models.ForeignKey(User, on_delete=models.CASCADE, related_name="received_messages")
    content = models.TextField()
    type = models.CharField(max_length=20, default="text")
    created = models.DateTimeField(auto_now_add=True)

📤 Sending Messages from Backend

from djwsbridge.utils import send_ws_message

# Example: send a message from a background task or signal handler
send_ws_message(
    user_id=5,
    data={"type": "text", "content": "Hello! This message was sent from the backend."}
)

🧪 Testing

Open Django shell:

python manage.py shell

Run:

from django.contrib.auth import get_user_model
from djwsbridge.utils import send_ws_message

User = get_user_model()
user = User.objects.get(id=1)

send_ws_message(user.id, {"type": "signals", "content": "Test message!"})

📃 License

This project is licensed under the MIT License.


👨‍💻 Author

  • Asadbek
  • GitHub: @asadbek
  • For issues or questions, please open an issue or submit a pull request.

⭐️ Support

If you find this project useful, please:

  • ⭐ Star the repository
  • 🛠 Provide feedback
  • 🔄 Submit suggestions or pull requests

A reliable, modular, and WebSocket-based real-time communication solution for Django.

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

djwsbridge-0.1.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

djwsbridge-0.1.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file djwsbridge-0.1.1.tar.gz.

File metadata

  • Download URL: djwsbridge-0.1.1.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for djwsbridge-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9c4f9e8c9676984d2b8a37a4192e9b904ecca03c9a0d57615eb1368c0f471da3
MD5 692433a72bb769de4accb66f8f4f25c3
BLAKE2b-256 58d846a9c65f7bbf8dc72d0c24a89527b5e21e46468e6e96d913aeb0910bc0c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for djwsbridge-0.1.1.tar.gz:

Publisher: publish.yml on asadbek000002/djwsbridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file djwsbridge-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: djwsbridge-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for djwsbridge-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 50668b36b8e34b714068f59f423f9eedfe94039e5a986bbd65be9af18d6cda67
MD5 7c70ae457abb8279a2ee852d26f5f230
BLAKE2b-256 8d776674dcb5ade51a54f516f1f38eeca430602e631daad8085a6ec11bb822d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for djwsbridge-0.1.1-py3-none-any.whl:

Publisher: publish.yml on asadbek000002/djwsbridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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