websocket-in-django
Simple PoC of a WebSocket server within a Django application.
Science Score: 44.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (11.7%) to scientific vocabulary
Keywords
Repository
Simple PoC of a WebSocket server within a Django application.
Basic Info
Statistics
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
- Releases: 0
Topics
Metadata Files
README.md
WebSocket-in-Django
Simple PoC of the integration of a loosely coupled WebSocket server within a Django application.
This demo is part of the "WebSocket Integration in Django" INAF technical report, available at https://hdl.handle.net/20.500.12386/31533.
Project Structure
The structure consists of a Django project created and set up properly according to the default and standard Django architecture. The WebSocket-related code is gathered into a single file, websocket.py, and it is composed of two classes: WebSocketHandler and WSServerWrapper. The aim of the former is, as suggested by its name, to manage the WebSocket interations with the clients (i.e., send messages, handle received messages, etc.), whilst the wrapper class is used just to wrap and run a SimpleWebSocketServer specifically configured for the handler class. The integration is completed by starting the WebSocket service in a new thread. This will ensure the WebSocket server to have the proper Django context and its own execution life within the Django application. Finally, a simple WebSocket JS client in the websocket.html file can be used to test the expected behaviour and play with the WS server.
Dependencies
This project is developed using the Django Python framework and the SimpleWebSocketServer module.
Usage
In order to run the proof-of-concept, clone this repository and install the requirements by opening your favourite Terminal, typing as follows:
sh
git clone https://github.com/tsumarios/WebSocket-in-Django
cd WebSocket-in-Django
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt
Then, you can run the Django application within the WebSocket server by typing the following command:
sh
python3 manage.py runserver --noreload [port]
Note that by default the Django server will be listening on port 8000 and the WebSocket server will be using port 5678. In order to change this setting, you need to set a new port value in the DEFAULT_WS_PORT variable in the settings.py file.
TLS
The WebSocket server can also run using the WSS (WebSocket Secure) protocol. Assuming the certificate and private key are in the base directory and named as "cert.pem" and "key.pem", just switch the ws_server in the websocket.py file from SimpleWebSocketServer to SimpleSSLWebSocketServer by uncommenting line 47 and commenting its next line. If the TLS protocol is enabled, remember to switch the WebSocket URL from ws://host:port/ to wss://host:port/ in line 24 in the websocket.html file or in your WebSocket client.
PoC
Eventually, once the servers are running you need to perform two last steps:
- Open the websocket.html file with your favourite browser and press the "connect" button (or any WebSocket client/plugin and connect to the following URL: ws://locahost:5678/). Note that if you send any message, the server will already echo it back.
- Open a new tab/window in your favourite browser and go to the following URL: http://localhost:8000/. The index page will show a simple "Hello, it's me..." text.
Whenever the index page is accessed - you can just press the refresh button (F5) in the browser - the custom update_signal is triggered and the WebSocket server, upon reacting to such signal, broadcasts a message to all its connected clients and you should be able to see the magic happen in the websocket.html page or, in general, in your WebSocket client.
Contacts
- Email: mario.raciti@inaf.it / marioraciti@pm.me
- LinkedIn: linkedin.com/in/marioraciti
- Twitter: twitter.com/tsumarios
Enjoy WebSockets!
Owner
- Name: Mario Raciti
- Login: tsumarios
- Kind: user
- Location: /dev/null
- Company: IMT School for Advanced Studies Lucca
- Website: https://tsumarios.github.io/
- Twitter: tsumarios
- Repositories: 6
- Profile: https://github.com/tsumarios
$~: 'PhD Student in Cybersecurity'
Citation (CITATION.cff)
cff-version: 1.2.0
title: WebSocket Integration in Django
message: Please cite this software using these metadata.
type: software
authors:
- given-names: Mario
family-names: Raciti
email: mario.raciti@inaf.it
affiliation: INAF - Osservatorio Astrofisico di Catania
orcid: 'https://orcid.org/0000-0002-7045-0213'
repository-code: 'https://github.com/tsumarios/WebSocket-in-Django'
GitHub Events
Total
- Fork event: 1
Last Year
- Fork event: 1
Dependencies
- Django ==4.0.7
- SimpleWebSocketServer ==0.1.1
- asgiref ==3.5.0
- sqlparse ==0.4.2