Git Product home page Git Product logo

django-websocketclient's Introduction

Django Websocketclient

django-websocketclient is a WebSocket client for Django to connect to the WebSocket server and listen to incoming messages in persistent mode

This library uses async interator for websocket connection provided by websockets library and wrap it under django's manage.py command. The idea is taken from the discussion thread on the django's forum.

Installation

Use the package manager pip.

pip install django-websocketclient

Or using poetry.

poerty add django-websocketclient

Setup

  1. Add websocketclient to INSTALLED_APPS in settings.py
INSTALLED_APPS = [
    "django.contrib.admin",
     ...
     ...
    "websocketclient",
]
  1. Set Host and async message handler path for WebSocket client in settings.py. This async handler will be called to process data received over WebSocket connection
WEBSOCKETCLIENT_HOST = "localhost:8000"
WEBSOCKETCLIENT_MESSAGE_HANDLER = "myproject.handlers.message_handler"
  1. Create handlers.py module at the root level of your project module. A typical django structure will looks like this.
myproject
├── 
├── myproject
│   ├── __init__.py
│   ├── asgi.py
│   ├── handlers.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
  1. Define async message handler coroutine under myproject/myproject/handlers.py
async def message_handler(message, websocket):
    # process message here
    print(message)
    
    # You can also send the message back to server
    await websocket.send("Message received from client")

Usage

Run manage.py command to connect to websocket server.

python manage.py runwebsocketclient

Optional CLI Arguments

usage: manage.py runwebsocketclient [--host HOST] [--path PATH]
  --host HOST   Websocket Server host
  --path PATH   Websocket Server path

Database operation inside message handler

DB operations like ORM query should be done using sync function wrap with sync_to_async

Available Settings

Variable Type Description
WEBSOCKETCLIENT_HOST String Websocket server host to conenct to. Required
WEBSOCKETCLIENT_PATH String Path part of websocket url. For e.g. events/
WEBSOCKETCLIENT_AUTH_HEADER String Authentication header key. Defaults to "Authorization"
WEBSOCKETCLIENT_TOKEN String Token for authenticated connection.
WEBSOCKETCLIENT_TOKEN_SCHEME String Token scheme to be used as a part of token. For e.g. 'bearer'
WEBSOCKETCLIENT_CONNECT_SECURE Boolean Set True to use wss:// protocol. Defaults to False
WEBSOCKETCLIENT_DISABLE_SSL_VERIFY Boolean Set True to disable SSL verification. Defaults to False

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

django-websocketclient's People

Contributors

kirankumbhar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.