Communication protocol that provides consistent connection between client and server.
Django HTTP
- django uses WSGI server to communicate with its browser
- WSGI server is synchronous
WebSocket
- asynchronous connection
- requests can be handled independently from each other, simultaneously
- need to use a ASGI server for this application
- ASGI server is a more modern versino of WSGI server that can handle both sync and async
Websocket flow
- Browser sends upgrade request to websocket → server responses with WS handshake
- WS handshake involves an exchange of security keys with hashing
- ASGI server does not come automatically with django and has to be installed manually
- in this case we use daphne
- there are alternatives like uvicorn or hyper corn
flow
browser → requests standard html → server responds → client request with htmx requesting for upgrade → server initiate websocket handshake
django channel
- django channel is unique for each individual user
- In order to broadcast message to many users we have to upgrade again we have to add a channel layer
- Channel layer can add individual channel to a group
- we need inmemory data store which manages the communication between all the different parts
- for development we can use inbuilt data store django channel provides
- for production better to use redis