Django Web Socket

JunePyo Suh·2020년 9월 7일
0
  1. Chat bots
  2. Real time dashboard
  3. Live tracking of IoT device

Web socket protocol was standardized in 2011 and all modern browsers provide built-in support for it. It establishes the tunnel between the server and the client. (Publisher and Subscriber)

Because Django does not have a default support for WebSocket, we need to use a third-party library, the Django Channels, built on ASGI specification.

ASGI and WSGI

WSGI

  • Python works in WSGI by default; WSGI is the Web Server Gateway Interface
  • A WSGI server only receives the request from the client and passes it to the application; it is provided as a standard for synchronous Python apps
    - does nothing else; all the details must be supplied by the application or the middleware
  • Doesn't allow for long-lived connections

ASGI

  • ASGI (Asynchronous Server Gateway Interface) is a successor to WSGI, which is intended to provide a standard interface between async-capable Python web servers, frameworks, and applications
  • ASGI is structured as a single, asynchronous callable
  • It takes scope, which contains details about the incoming request and allow awaitable functionality
  • Allows multiple incoming and outgoing events
  • Supports normal HTTP as well

Django Channels

An extension for Django projects, Django Channels adds the functionality of asynchronous code underneath and through Django's synchronous core, allowing Django projects to handle not only HTTP, but protocols that require long-running connections such as WebSockets, MQTT, chatbots, and etc.

Django Channels effectively integrates with DJango's auth systems, session system, and more.

Consumer

  • Consumers to channels are views to Django
  • Basic unit of Channels code; can be thought of a little application on its own
  • When a new request or new socket comes in, Channels will follow its routing table to find the right consumer for that incoming connection and start up a copy of it
  • Http requests can also be served by consumers
  • To sum up, Django Views are for short live requests, while Consumers are for long living requests

Scope and Events

A scope contains information about the path of connection, IP address, headers, and content data. The life cycle of scope is illustrated below:

Libraries Needed

  • Install Django Channels --> pip install -U channels
  • websockets --> pip install websockets
  • websocket-client

consumer.py

import asyncio
import json
from django.contrib.auth import get_user_model
from channels.consumer import AsyncConsumer
from channels.db import database_sync_to_async

from .models import Trhead, ChatMessage

class ChatConsumer(AsyncConsumer):
    async def websocket_onnect(self, event):
        print("connected", event)

    async def websocket_receive(self, event):
    // when a message is received from the websocket
    
    async def websocket_disconnect(self, event):
    // when the socket disconnects

routing.py

You are not overriding regular HTTP methods.
Because websocket is a different protocol than HTTP, so you can open up both websocket and HTTP communications.

Integrating Redis

To connect different users even on the same computer or different computers, we need to have a data store of events that are happening. To do this, you must install Redis, which is a messaging queue for events that happen. Then Channels consume these events from Redis.

medium article on django websockets

Oddbird websockets

0개의 댓글