[PyConAR 2017] Django Channeled

[PyConAR 2017] Django Channeled

Instead of just receiving and answering requests – one by one – Channels changes the way Django works, towards an event-driven approach. With that, we now have access to WebSockets and others asynchronous wonders. Event driven for the win!

In this talk, we are going to see the asynchronous concepts and who to iplement them with Django Channels.

The D is silent.

67e79c99f3b6e205ebc68a6ab91545ad?s=128

Jonatas Baldin

November 18, 2017
Tweet

Transcript

  1. 1.

    DJANGO C H A N N E L E D

    “jojo” @jonatasbaldin
  2. 3.

    client makes a request server process it and sends a

    response client displays the response
  3. 4.

    client makes a request Django receives an HTTP Request routes

    it to a View which returns an HTTP Response client displays the response
  4. 5.

    In 2017, web apps need to know how to display

    a feed in your timeline, from thousands of people around the world, as soon as they publish new content, in less than one second
  5. 9.

    WebSocket is a bidirectional and message-oriented transport layer, allowing clients

    and servers to exchange data using a persistent connection
  6. 10.

    WebSocket is a bidirectional and message-oriented transport layer, allowing clients

    and servers to exchange data using a persistent connection
  7. 11.

    WebSocket is a bidirectional and message-oriented transport layer, allowing clients

    and servers to exchange data using a persistent connection
  8. 12.

    WebSocket is a bidirectional and message-oriented transport layer, allowing clients

    and servers to exchange data using a persistent connection
  9. 18.

    DJANGO IS AN OLD FRAMEWORK SOLVING OLD PROBLEMS BUT IT

    CAN BE EXTENDED TO SOLVE NEW ONES
  10. 19.
  11. 26.

    As WSGI doesn’t understand WebSockets, a new spec was created

    – ASGI – which basically replaces WSGI
  12. 29.

    Daphne is executed like any other WSGI server, just run

    the command $ daphne project.asgi:channel_layer
  13. 32.
  14. 38.

    # consumers.py def ws_connect(message): message.reply_channel.send({'accept': True}) def ws_message(message): message.reply_channel.send(message[‘text’]) #

    routing.py channel_routing = [ route('websocket.connect', ws_connect), route('websocket.receive', ws_message), ]
  15. 39.
  16. 40.

    # consumers.py def ws_connect(message): Group('tweets').add(message.reply_channel) # models.py class Tweet(models.Model): text

    = models.CharField(max_length=140) def save(self, *args, **kwargs): result = super().save(*args, **kwargs) Group('tweets').send({'text': self.text}) return result # routing.py channel_routing = [ route('websocket.connect', ws_connect), ]
  17. 43.

    # settings.py CHANNEL_LAYERS = { 'default': { 'BACKEND': 'asgi_redis.RedisChannelLayer', 'CONFIG':

    { 'hosts': [(REDIS_HOST, 6379)], }, 'ROUTING': 'wsquiz.routing.channel_routing', } }
  18. 49.

    To develop the client – like a JavaScript application –

    Channels comes with a library called WebSocketBridge
  19. 51.

    Worker (Django management command) Channels Backend (routing and datastore) client

    (WebSocketBridge) message message ASGI (the new WSGI)
  20. 57.

    Worker (Django management command) Channels Backend (routing and datastore) client

    (WebSocketBridge) message message ASGI (the new WSGI - Daphne)
  21. 69.
  22. 72.

    DJANGO C H A N N E L E D

    @jonatasbaldin
  23. 73.

    DJANGO C H A N N E L E D

    @jonatasbaldin the D is silent