What are websockets? WebSocket is a computer communications protocol, providing...
A
Ajith Kumar P M
@ajth-in
Nov 15 '21
3 min read
23 reactions
2 comments
#beginners#programming#python#websockets
What are websockets?
WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection.
Introduction of Asynchronous Programming in python.
Unlike Javascript asynchronous execution wasn't supported in python. And this was a huge drawback especially in case of web development, since asynchronous servers and apps are essential in developing web apps.But with the introduction of async/await to python 3.5, things started to change.
asyncio
In synchronous execution the program executes sequentially in synchronous to the processor clock.
Coroutines are computer program components that generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed.
(threading is preemptive)
Subroutines are special cases of coroutines.[3] When subroutines are invoked, execution begins at the start, and once a subroutine exits, it is finished; an instance of a subroutine only returns once, and does not hold state between invocations. By contrast, coroutines can exit by calling other coroutines, which may later return to the point where they were invoked in the original coroutine;
Coroutines are very similar to threads. However, coroutines are cooperatively multitasked, whereas threads are typically preemptively multitasked. Coroutines provide concurrency but not parallelism. The advantages of coroutines over threads are that they may be used in a hard-realtime context (switching between coroutines need not involve any system calls or any blocking calls whatsoever),
python
Generators, also known as semicoroutines (not discussed here)
asyncio python example.
python
So having able to write native coroutines in python opens the door to asynchronizing python web servers and apps .
ASGI
In WSGI applications takes a single request and returns response at a time. This single and synchronous callable limits WSGI for long lived connections like websocket connections.
source1source2ASGI consists of two different components:
A protocol server, which terminates sockets and translates them into connections and per-connection event messages.
An application, which lives inside a protocol server, is instanciated once per connection, and handles event messages as they happen.
ASGI relies on the following mental model: when the client connects to the server, we instanciate an application. We then feed incoming bytes into the app and send back whatever bytes come out.
How to Add Websockets to a Django App
There will be Django defined ASGI application function in asgi.py file.
python
And we need to create a wrapper for the django provided asgi application.
python
defining the websockets function
python
For running the asgi app we require an asynchronous web server (daphene or uvicor)
in case of uvicorn :
bash
The following js script can be used for testing the code.