WebSocket in Django
A WebSocket is a standard protocol for two-way data transfer between a client and server. The WebSockets protocol does not run over HTTP, instead it is a separate implementation on top of TCP.
WebSockets enable the server and client to send messages to each other at any time, after a connection is established, without an explicit request by one or the other. This is in contrast to HTTP, which is traditionally associated with the challenge-response principle — where to get data one has to explicitly request it. In more technical terms, WebSockets enable a full-duplex connection between the client and the server.
Websocket is a duplex protocol and works on bidirectional communication.Its URL starts with ws:// or wss:// (stands for WebSocket or WebSocket Secure).
HTTP is a protocol which allows the fetching of resources, such as HTML documents. It is the foundation of any data exchange on the Web and it is a client-server protocol, which means requests are initiated by the recipient, usually the Web browsers.
How’s the Websocket is different from traditional HTTP protocols. Websockets use bi-directional communication and the connection will not break until the client or server decides to terminate the connection.
The life cycle of Websocket?
Handshake
Websocket is also an application layer protocol and it is an HTTP upgrade that uses the same TCP connection over ws://
In simple terms client asks for the server that, can we make a WebSocket connection and in reply server will say, yeah buddy let’s upgrade the connection to WS
This is called the handshake and the connection is established between client and server.
Open and Persistent Connection
Once the connection is established communication happens in terms of bi-directional message communication.
Connection Closed
Once the connection is established it stays forever until the client or server wants to terminate the connection.
Enough of the theory, let’s dive into the implementation. So to have a WebSocket connection we first need to have a client and a server. For the implementation, we are using Python’s Django Server that is a microframework.
Django Channel?
Channels is a project that takes Django and extends its abilities beyond HTTP - to handle WebSockets, chat protocols, IoT protocols, and more. It’s built on a Python specification called ASGI.
Channels builds upon the native ASGI support available in Django since v3.0, and provides an implementation itself for Django v2.2. Django still handles traditional HTTP, whilst Channels give you the choice to handle other connections in either a synchronous or asynchronous style.
Consumers
While Channels is built around a basic low-level spec called ASGI, it’s more designed for interoperability than for writing complex applications in. So, Channels provides you with Consumers, a rich abstraction that allows you to make ASGI applications easily.
领英推荐
Consumers do a couple of things in particular:
There are two types of Consumer?
As the name suggests, Synchronous Consumer can write synchronous codes. Sync Consumer will accept multiple connections and be able to message all of them but only able to manage one request at a time, others have to wait until the first is finished.
It is similar to how Django handles HTTP requests.
2. Asynchronous Consumer:
Asynchronous Consumer‘s code will work async, This Consumer will also accept multiple requests and is also able to handle all the requests simultaneously. For the DB operations, we have to use an adaptor to convert async to sync because DB operations can not be performed asynchronously.
How Do Channels Work ?
Firstly we have to create a Django Project or we can integrate channels in existing projects of django . For integrate we have to follow following Steps?
pip install channels
2. Add Channels in installed app of django Project
INSTALLED_APPS = (
? ? ...
? ? 'channels',
)
3. Add Following details in asgi.py file which present in main project folder
import os
from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
application = ProtocolTypeRouter({
? ? "http": get_asgi_application(),
? ? # Just HTTP for now. (We can add other protocols later.)
})
4. Add ASGI server in settings.py file as project_name.asgi.application
ASGI_APPLICATION = 'urgidoctor.asgi.application'
5. Try to run server and you’ll see the ASGI is running without any error
Conculsion : WebSocket is a naturally full-duplex, bidirectional, single-socket connection. With WebSocket, your HTTP request becomes a single request to open a WebSocket connection and reuses the same connection from the client to the server, and the server to the client.
Hope this will helpful :)
Thanks !