Search code examples
pythondjangochattornado

Why do chat applications have to be asynchronous?


I need to implement a chat application for my web service (that is written in Django + Rest api framework). After doing some google search, I found that Django chat applications that are available are all deprecated and not supported anymore. And all the DIY (do it yourself) solutions I found are using Tornado or Twisted framework.

So, My question is: is it OK to make a Django-only based synchronous chat application? And do I need to use any asynchronous framework? I have very little experience in backend programming, so I want to keep everything as simple as possible.


Solution

  • Django, like many other web framework, is constructed around the concept of receiving an HTTP request from a web client, processing the request and sending a response. Breaking down that flow (simplified for sake of clarity):

    1. The remote client opens TCP connection with your Django server.
    2. The client sends a HTTP request to the server, having a path, some headers and possibly a body.
    3. Server sends a HTTP response.
    4. Connection is closed
    5. Server goes back to a state where it waits for a new connection.

    A chat server, if it needs to be somewhat real-time, needs to be different: it needs to maintain many simultaneous open connections with connected clients, so that when new messages are published on a channel, the appropriate clients are notified accordingly.

    A modern way of implementing that is using WebSockets. This communication flow between the client and server starts with a HTTP request, like the one described above, but the client sends a special Upgrade HTTP request to the server, asking for the session to switch over from a simple request/response paradigm to a persistent, "full-duplex" communication model, where both the client and server can send messages at any time in both direction.

    The fact that the connections with multiple simultaneous clients needs to be persistent means you can't have a simple execution model where a single request would be taken care of by your server at a time, which is usually what happens in what you call synchronous servers. Tornado and Twisted have different models for doing networking, using multithreading, so that multiple connections can be left open and taken care of simultanously by a server, and making a chat service possible.


    Synchronous approach nevertheless

    Having said that, there are ways to implement a very simple, non-scalable chat service with apparent latency:

    1. Clients perform POST requests to your server to send messages to channels.

    2. Clients perform periodical GET requests to the server to ask for any new messages to the channels they're subscribed to. The rate at which they send these requests is basically the refresh rate of the chat app.

    With this approach, your server will work significantly harder than if it had a asynchronous execution model for maintaining persistent connections, but it will work.