Skip to main content

Helpers for using WebSockets in Django

Project description

django-ws

Helpers for using WebSockets in Django

Installation

pip install django-ws

Setup

asgi.py

Remove line: from django.core.asgi import get_asgi_application Remove line: application = get_asgi_application()

Add to the end:

from django_ws import get_websocket_application

application = get_websocket_application()

ws_urls.py

Next to your root urls.py create a ws_urls.py like the example below that uses your websocket.

from django.urls import path

import myapp.ws

urlpatterns = [
  path('ws', myapp.ws.MySocket),
]

Write a WebSocket

from django_ws import WebSocketHandler

class MySocket(WebSocketHandler):
  async def on_open(self):
    do_something_on_open()

  async def on_message(self, data):
    do_something_on_msg()

    # send json data
    self.send({"reply": "sending data back"})

  async def on_close(self):
    do_something_on_close()

More Features

start_ping method

coming soon

start_task method

coming soon

sleep_loop method

coming soon

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_ws-0.7.0.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distribution

django_ws-0.7.0-py3-none-any.whl (4.7 kB view hashes)

Uploaded Python 3

Supported by

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