Top 10 Interactive Website Design Trends in 2024.pptx
The State of WebSockets in Django
1. The State of WebSockets in Django
Rami Sayar - @ramisayar
Technical Evangelist
Microsoft Canada
2. Agenda
• Where is WebSockets right now?
• Where is Socket.IO?
• Where does Django fit in?
• Using Django SwampDragon.
• Using Gevent.
CONFOO - @RAMISAYAR
4. What is a production web app?
• Django is surrounded by tons
of applications, services and
code in production.
• Advanced Django architecture
is needed to support Django
in production without failing
hard.
• WebSockets falls apart from
the rest.
CONFOO - @RAMISAYAR
5. WebSockets
• “WebSocket is a protocol for
full-duplex communication
channels over a single TCP
connection.” Wikipedia
• Image Reference
CONFOO - @RAMISAYAR
7. Socket.IO
• “Socket.IO enables real-time
bidirectional event-based
communication. It works on
every platform, browser or
device, focusing equally on
reliability and speed.”
Reference
• Use by everyone: Microsoft
Office, Yammer, Zendesk,
Trello, etc…
CONFOO - @RAMISAYAR
10. Python and WebSockets
• Python and WebSockets has notoriously been very difficult.
• Two reasons:
• WSGI protocol does not support WebSockets. Most popular form of
web request handling in the Python web apps. Most frameworks are
designed for use with FastCGI or mod_wsgi, etc.
• Default Python networked apps not exactly designed for event-based
communication, you have to use things like gevent and twisted/tornado.
CONFOO - @RAMISAYAR
11. Django & WebSockets
• Django applications complete HttpResponses.
• Django not well suited for WebSocket event handling structure
nor the channel approach in Socket.IO.
CONFOO - @RAMISAYAR
13. gevent
• “gevent is a coroutine-based Python networking library that
uses greenlets to provide a synchronous API on top of the libev
event loop.”
• Event loop based on libev (same as Node.js)
• Lightweight execution units, a.k.a. “lightweight threads”.
• Cooperative sockets with SSL support.
• Similar programming model to Node.js.
CONFOO - @RAMISAYAR
14. gevent-socketio
• gevent-socketio is a pure Python implementation of the
Socket.IO protocol. Basically, it is a port of Socket.IO and
typically behind in major releases.
• Designed to work with multiple different WSGI-based web
frameworks.
• Handles the “WSGI” problem by providing a custom gevent
WSGI server for use with gevent-socketio.
• Only dependencies are gevent.
• Imperfect solution: Django is still blocking (monkey
patching is not perfect).
CONFOO - @RAMISAYAR
16. “Native” Django Implementation
• Previous approach requires significant modifications to your
current Django app serving stack. Native as in you don’t need to
know anything besides Django.
• Building an implementation of WebSockets on top of Django.
• Identified two problems for WebSockets in Django:
• Problems with WSGI still present EXCEPT in uWSGI.
• Networked event handling model is problematic.
• Solution:
• Use Nginx which follows – event-driven async architecture.
• uWSGI & Gevent for event handling.
CONFOO - @RAMISAYAR
17. Django-WebSocket-Redis
• Module implements websockets on top of Django.
• Use Redis as a data store for messaging behind uWSGI and Nginx.
• Runs a separate Django main loop in a cooperative concurrency
model using gevent -> 1 thread for all open websockets.
• Django can be started as usual with ‘./manage.py runserver’.
• Normal requests communication with separate main (websocket)
loop through Redis.
• Read:
Offloading Websockets and Server-Sent Events AKA “Combine
them with Django safely”
CONFOO - @RAMISAYAR
19. SwampDragon
• New project that adds websocket support
• Self-publishing models
• Django ORM Style Data Filter for Subscription Channels –
Similar to WebSockets + Data Filter
• Relies on Redis for messaging + Tornado
• Angular JS Support
CONFOO - @RAMISAYAR
21. On Redis
• Redis is extremely popular as an easy and simple publish-
subscribe.
• Redis is very particular about memory. Running out of memory
in Redis -> CRASH AND BURN!
• If you don’t have enough memory in your VM to run Redis
Cache -> Change your approach, use Apache Kafka.
CONFOO - @RAMISAYAR
22. What did we learn?
• The State of WebSockets in Django.
• Django and WebSockets is not impossible.
CONFOO - @RAMISAYAR