Skip to content

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

License

Notifications You must be signed in to change notification settings

eamigo86/django3_asgi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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'"

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published