> For the complete documentation index, see [llms.txt](https://olee-tech.gitbook.io/django/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://olee-tech.gitbook.io/django/channels.md).

# channels

### <https://medium.com/gitconnected/step-by-step-django-channels-b17a58141de1> <a href="#aead" id="aead"></a>

### Synchronous vs Asynchronous <a href="#aead" id="aead"></a>

Let’s first look at what synchronous and asynchronous work is in terms of Django.

Django works synchronously. That is, an HTTP request is handled completely synchronously. A request is sent, waited and the response is returned.

<figure><img src="https://miro.medium.com/v2/resize:fit:720/format:webp/1*OtZRh7w9rRSU4mdkwIsVkg.png" alt=""><figcaption></figcaption></figure>

The server is not able to send a single message to the client. In a synchronous operation, there should be a request to get a response.

In asynchronous operation, we launch a request. There is no waiting for a response, the app continues to serve the user and executes the tasks.

### Websockets <a href="#id-39aa" id="id-39aa"></a>

Websockets is a protocol that can establish two-way communication between the browser and the server (HTTP is a one-way protocol). A client can send a message to a server and receive a message about the relevant event without having to wait for a response. Both parties can communicate with each other independently at the same time.

<figure><img src="https://miro.medium.com/v2/resize:fit:249/1*CvnWilhM1fqR_p36elMyhg.png" alt="" height="229" width="249"><figcaption><p>A diagram describing a connection using WebSocket. Source: <a href="https://en.wikipedia.org/wiki/WebSocket">Wikipedia</a></p></figcaption></figure>

Websocket is a stateful protocol, meaning the connection between client and server stays alive until terminated by one of the parties (client or server). After closing the connection by either client or server, the connection is terminated from both ends.

<br>

<figure><img src="https://miro.medium.com/v2/resize:fit:700/1*1ouOIXiEIXmkgobHAKVeTg.png" alt="" height="524" width="700"><figcaption><p>Websocket Connection. Image by the author.</p></figcaption></figure>

In the first step, the client sends an HTTP request to the server. It asks the server to open a WebSocket connection. The server accepts it and returns a 101 switching protocols response. At this point, the handshake is completed. TCP/IP connection is left open and both sides can send messages. The connection remains open until one of them drops off. This process is often referred to as full-duplex.

<figure><img src="https://miro.medium.com/v2/resize:fit:700/1*rrj_qHg4w0eXDs0L6ExvtA.png" alt="" height="375" width="700"><figcaption><p>Comparison of HTTP and Websockets in Django App. Image by the author.</p></figcaption></figure>

When a client sends an HTTP request, it is received by a Django application via WSGI (web server gateway interface). It ends up in Django’s URL and is routed to Django’s view.

For WebSockets, ASGI (asynchronous server gateway interface) is in charge instead of WSGI. And it is routed to a consumer instead of a view.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://olee-tech.gitbook.io/django/channels.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
