Git Product home page Git Product logo

django3_asgi's Introduction

django3_asgi

This is a test project of how to implement WebSockets with Django >= 3.0 without the need for any extra libraries:

NOTE: Require Python 3.6 or higher

How test it:

1. Create a folder, clone the repo, create a virtualenv and install the required packages:

mkdir django_websockets && cd django_websockets
python -m venv venv
source venv/bin/activate
git clone https://github.com/eamigo86/django3_asgi && cd django3_asgi
pip install -r requirements.txt

2. Run Django migrations, create a superuser, and collect the static files (optional for admin only):

./manage.py migrate
./manage.py createsuperuser
./manage.py collectstatic

3. Run the server in any available port:

uvicorn --port 8000 websocket_app.asgi:application --reload

NOTE: After a client connects to the server via WebSocket, the server will start sending a message every X seconds (1 <= X <= 6)

4. Navigate to your admin site or connect to your db (sqlite by default) with any DB Manager app and create some Car objects.

5. Open a new browser tab, go to Developer Tools, open the Console tab and we will proceed to test the websocket server as follows:

// This is to simulate a client application. 

ws = new WebSocket("ws://localhost:8000/"); //We create a new connection to our server specifying the protocol, address, and port
ws.onmessage = event => console.log(event.data); //We define the behavior that our client will have with the incoming messages

// Send some message to the websocket server
ws.send("ping"); // receive "pong!"

ws.send("car"); // receive the plate value for the first car object in db
ws.send("car:3"); // receive the plate value for the car with id=3 in your db
ws.send("car:1000"); // receive "Do not exists any car in the with id '1000'"

django3_asgi's People

Contributors

dependabot[bot] avatar eamigo86 avatar

Watchers

 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.