Reinventing Django for the Real-Time Web

Reinventing Django for the Real-Time Web

A talk I gave at PyCon US 2016.

077e9a0cb34fa3eba2699240c9509717?s=128

Andrew Godwin

May 30, 2016
Tweet

Transcript

  1. 9.

    ???

  2. 12.
  3. 15.

    Channel Layer Interface Server Worker Server Process 1 ASGI ASGI

    Asynchronous socket handling Synchronous Django project Interface Server Worker Server ASGI ASGI Worker Server ASGI Process 2 Process 3 Process 4
  4. 16.

    Channel Layer Worker Server Interface Server Channel Layer Worker Server

    Interface Server Channel Layer Worker Server Interface Server Channel Layer Worker 1 Worker 2 Worker 3 Redis
  5. 27.
  6. 29.

    Liveblog We want people to get new blog posts as

    they are published, without refreshing
  7. 30.

    Liveblog People open a WebSocket when they open the page

    Their WebSocket is added to a group When the BlogPost model is saved, we send the post to that group
  8. 31.

    Their WebSocket is added to a group When the BlogPost

    model is saved, we send the post to that group def ws_connect(message): Group("liveblog").add(message.reply_channel) class BlogPost(models.Model): ... def save(self, *args, **kwargs): ... Group("liveblog").send({ "text": json.dumps({"id": self.id}), })
  9. 33.

    Chat When people connect they join a chat group When

    we receive a message we send it to the group
  10. 34.

    When people connect they join a chat group def ws_connect(message):

    Group("chat").add(message.reply_channel) When we receive a message we send it to the group def ws_receive(message): Group("chat").send({ "text": message["text"], })
  11. 35.

    ...and we tell Django what consumers are joined to which

    actions. # in routing.py routing = [ route("websocket.connect", consumers.ws_connect), route("websocket.receive", consumers.ws_receive), ] ...and we tell Django what consumers are joined to which actions.
  12. 36.

    Important notes Runserver just works with WebSockets now Generic Consumers

    exist Fully worked versions of these two are at github.com/andrewgodwin/channels-examples Django sessions + auth work with WebSockets
  13. 37.

    FIFO queue with send and receive_many operations, named with a

    string. Channel Group Named set of channels with add/remove/send operations Cross-process Messages Representations for HTTP and WebSocket sessions
  14. 40.

    Channel Layer Interface Server Worker Server Process 1 ASGI ASGI

    Asynchronous socket handling Synchronous Django project Interface Server Worker Server ASGI ASGI Worker Server ASGI Process 2 Process 3 Process 4
  15. 41.

    Channel Layer Interface Server Worker Server Channel Layer Daphne HTTP

    + WS WSGI Adapter HTTP asgi_redis Cross-network, shards asgi_ipc Single-machine Django Consumer system WSGI Adapter Most WSGI apps
  16. 42.

    WSGI and/or ASGI An ASGI system can serve HTTP and

    WebSocket ...or your WSGI system can send onto channels
  17. 43.

    Channel Layer Worker Server Interface Server Channel Layer Worker Server

    Interface Server WSGI App WSGI Server Channel Layer Worker 1 Worker 2 Worker 3 Redis
  18. 44.