Django

and

WebSockets

Nicolas FERRARI (alwaysdata.com) - April 2017

12 slides ~ 15/20 minutes

WebSockets ?

  • HTTP request/response Paradigm
  • Persistent connection need:
    • AJAX (2005), long polling
  • WebSockets: RFC 6455 (dec. 2011)

Django & WSGI

Normalized communication

 

Does not support WebSockets (or HTTP2)

The solution?

django-channels

  • Official Django project (Andrew Godwin, Sept. 2016)
  • Provide an alternative to WSGI: ASGI

Simple example

Django setup

Project setup

  • Model Message (subject, body)
  • ./manage.py makemigrations/migrate
  • message list (simple url, view and template)

 

WebSockets setup

Bonus

More features

  • Redis channel layer
  • Group (e.g. chat groups)
  • Friendly decorators :
    • Access session
    • Enforce ordering

That's all folks!

Here are some resources:

  • http://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet

  • https://www.html5rocks.com/en/tutorials/websockets/basics/

  • http://sametmax.com/quest-ce-que-wsgi-et-a-quoi-ca-sert/

  • https://tools.ietf.org/html/rfc6455
  • https://www.djangoproject.com/weblog/2016/sep/09/channels-adopted-official-django-project/
  • http://natim.github.io/django-slides/channels/
  • https://channels.readthedocs.io

WebSockets and Django

By Nicolas Ferrari

WebSockets and Django

Work with WebSockets and Django, thanks to django-channels

  • 1,971