Q & A
What are websocket? Why do we need it?
Quoting from wikipedia,
"WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API in Web IDL is being standardized by the W3C."
WebSockets are used for streaming messages. To implement real-time notification we need to stream messages from the server to the client without a refresh or making an HTTP request from the client. Hence websockets.
How do we implement websockets in django?
We will be using django-channels (channels) for websockets. Django, by default does not support websocket. Channels is a django project which allows Django to handle websockets, HTTP and HTTP2 requests. But how does channels implement websockets in WSGI server (gunicorn) which does not support websockets. Simple we don't. Instead, we will be using Daphne, an interface server designed for django-channels. Deploying channels on Daphne is fairly easy (Tutorial). In the tutorial, you will find this, "ASGI and its canonical interface server Daphne are both relatively new, and so you may not wish to run all your traffic through it yet (or you may be using specialized features of your existing WSGI server)". As stated its not recommended to use Daphne for all the traffic.
When we say WSGI (Web Server Gateway Interface) or ASGI (Asynchronous Server Gateway Interface), these are specifications (explained in detail here and here). The difference between the 2 is WSGI was designed for HTTP request-response type cycle, whereas ASGI is much boarder. (Read more)
Why Daphne?
Simple answer. It is developed for django-channels, always easier to go with tools built for each other. Better not to re-invent the wheel. Right!
"Apache as reverse proxy". What is reverse proxy?
This stackoverflow answer explains it the best.
What are websocket? Why do we need it?
Quoting from wikipedia,
"WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API in Web IDL is being standardized by the W3C."
WebSockets are used for streaming messages. To implement real-time notification we need to stream messages from the server to the client without a refresh or making an HTTP request from the client. Hence websockets.
How do we implement websockets in django?
We will be using django-channels (channels) for websockets. Django, by default does not support websocket. Channels is a django project which allows Django to handle websockets, HTTP and HTTP2 requests. But how does channels implement websockets in WSGI server (gunicorn) which does not support websockets. Simple we don't. Instead, we will be using Daphne, an interface server designed for django-channels. Deploying channels on Daphne is fairly easy (Tutorial). In the tutorial, you will find this, "ASGI and its canonical interface server Daphne are both relatively new, and so you may not wish to run all your traffic through it yet (or you may be using specialized features of your existing WSGI server)". As stated its not recommended to use Daphne for all the traffic.
There are 2 solutions:
- We can ignore the warning and go ahead with Daphne. This is the easier solution and probably will work for small number of users.
- We can use Apache for HTTP requests and Daphne for websockets.
I like solution 2. For Daphne, Apache acts like a reverse proxy, redirecting all the websocket requests to Daphne server which is running on a different port. To setup Apache WSGI server for GraphSpace follow the steps here. Now we need to setup Apache as a reverse proxy, follow these steps:
- Run the following terminal command:
- a2enmod rewrite
- a2enmod proxy
- a2enmod proxy_wstunnel
- Inside the 000-default.conf, copy and paste following lines inside <VirtualHost *:80> </VirtualHost>
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://127.0.0.1:9099%{REQUEST_URI} [P,QSA,L]
Update port and host according to Daphne port.
The above commands checks for "UPGRADE" attribute in a the request from a client and reroutes it to Daphne if it is set to websocket.
Now you have a WSGI server running in Apache and a Daphne server for websockets. Enjoy!
What is WSGI and ASGI? What is the difference?When we say WSGI (Web Server Gateway Interface) or ASGI (Asynchronous Server Gateway Interface), these are specifications (explained in detail here and here). The difference between the 2 is WSGI was designed for HTTP request-response type cycle, whereas ASGI is much boarder. (Read more)
Why Daphne?
Simple answer. It is developed for django-channels, always easier to go with tools built for each other. Better not to re-invent the wheel. Right!
"Apache as reverse proxy". What is reverse proxy?
This stackoverflow answer explains it the best.
Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating .
ReplyDeleteDigital Marketing Training in Chennai
Digital Marketing Course in Chennai
kayseriescortu.com - alacam.org - xescortun.com
ReplyDeleteperde modelleri
ReplyDeletesms onay
mobil ödeme bozdurma
Nft nasıl alinir
Ankara evden eve nakliyat
trafik sigortasi
Dedektör
web sitesi kurma
aşk kitapları