Djangocon: building real-time apps with django - Iacopo Spalletti

Tags: djangocon, django

(One of my summaries of a talk at the 2018 european djangocon.)

The web isn’t a pure request/response web anymore. There’s real-time web now. There are lots of tools (yeah!), but you also get lots of complexity (booo!).

Django channels is a framework to use django in the non-http world. Websockets, but not only those. Channels (version 1) is out now for three years, version 2 came out a few months ago. The talk assumes version 2.

Some concepts:

  • Asynchronous. For us, this means event-driven. A problem is that asynchronous code is normally harder to understand because of its non-linear nature.

    But… channels hides most of the complexity for you.

  • ASGI. A protocol spec (like WSGI), independent of channels.

    It is ASGI all the way down. You can nest them.

  • Protocol server. This implements the ASGI spec for a specific protocol. It interacts with the connection. It bridges the network and the application.

    For channels, this often is ‘daphne’.

  • Routing. Maps incoming messages to consumers. It is nestable and composable.

  • Scope. A “scope” is created for every connection. It holds the data of the connection.

  • Channel. IPC mechanism (“Inter Process Communication”). FIFO “at-most-once” queue.

  • Consumers. The core abstraction to build an ASGI app. It handles events on a connection. It is stateful: every individual connection is connected to an individual consumer.

    Note on stateful: everything runs in a single process.

  • Events. Triggered during a “scope”. In the end, consumers consume events.

  • Frontend. Often: websockets, which means javascript.

He showed a demo and then some code. (I couldn’t type over the example code, of course :-) ).

  • First you’ll define “channel layers” in your settings. A queue somewhere, for instance.
  • Next some routing. Paths are mapped to consumers.
  • Next some consumers. For instance a websocket consumer. You can have methods like connect(), disconnect() and receive(... event ...).
  • A bit of javascript for the websocket frontend.

Summary of channels:

  • Async is made easier.
  • You still have access to the django API.
https://abload.de/img/screenshot2018-02-25ancs0v.png

Photo explanation: interiors means something like this. The office of the manager.

blog comments powered by Disqus
 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):