# Web Sockets - Streaming app

**URL:** <https://forum.djangoproject.com/t/web-sockets-streaming-app/38106>\
**Category:** Async/Channels\
**Created:** [January 21, 2025, 8:58am UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106 "2025-01-21T08:58:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 21, 2025, 8:58am UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/1 "2025-01-21T08:58:38Z")

</div>

Hi,

Im after some advice.

I want to build a kind of terminal app, where content will be loaded in the screen in realtime, or as close as. This will be based on comments from users. bit like a chat system.

I’ve looked and django channels, but not clear on how i get the output to the screen?

So wasn’t sure if anyone could point me in the right direction?

Thanks  
Tom

---

<div class="post-metadata">

**Author:** ![matthiask](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matthiask/32/115_2.png) [@matthiask](https://forum.djangoproject.com/u/matthiask)\
**Post date:** [January 21, 2025, 9:57am UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/2 "2025-01-21T09:57:06Z")

</div>

I have used the `websockets` library in the past [https://websockets.readthedocs.io/](https://websockets.readthedocs.io/) to connect to the server endpoint implemented using Django channels. Other libraries such as httpx-ws exist but I haven’t used them. You could use such a library to implement a terminal program.

If you want to use a webbrowser as a client you could start here:

> **[Writing WebSocket client applications - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications)**
>
> WebSocket client applications use the WebSocket API to communicate with WebSocket servers using the WebSocket protocol.

A different way to go would be to use SSE (server-sent events) instead of Websockets, if you don’t need bidirectional communication:

> **[Using server-sent events - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)**
>
> Developing a web application that uses server-sent events is straightforward. You'll need a bit of code on the server to stream events to the front-end, but the client side code works almost identically to websockets in part of handling incoming...

Many possibilities as always!

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 21, 2025, 10:19am UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/3 "2025-01-21T10:19:46Z")

</div>

Thanks for the reply, matthiask.

Server Side Events might be the way to go. When the comments come in i want to be able to make them as complete for example. So i was hoping to render a button on the screen. Would Server Side Events allow this?

Sorry, if thats a stupid question, its just i’ve never worked on a terminal like interface before

---

<div class="post-metadata">

**Author:** ![matthiask](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matthiask/32/115_2.png) [@matthiask](https://forum.djangoproject.com/u/matthiask)\
**Post date:** [January 21, 2025, 10:50am UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/4 "2025-01-21T10:50:23Z")

</div>

> [@TommyQuality](#):
>
> So i was hoping to render a button on the screen. Would Server Side Events allow this?

Yes, sure. You’d have to send the completion event as a form post though, you cannot use the SSE channel because SSE is unidirectional.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 21, 2025, 11:05am UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/5 "2025-01-21T11:05:18Z")

</div>

Ok, so post the form back to mark the comment as completed, and if i was to have a field on my model, as completed true/false then i could reflect this on the page being fed back from the SSE?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 21, 2025, 11:51am UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/6 "2025-01-21T11:51:31Z")

</div>

> [@TommyQuality](#):
>
> I’ve looked and django channels, but not clear on how i get the output to the screen?

That depends upon what the data is that you are sending back through the channel.

If you look at the Django Channels chat tutorial, it’s sending text - which the JavaScript client adds to a div.

I use HTMX with the websocket extension. That allows me to send an HTML element from the server, and HTMX will then inject it into the corresponding spot on the page.

A basic proof-of-concept example using the chat tutorial as a base is located at [GitHub - KenWhitesell/htmx\_channels](https://github.com/KenWhitesell/htmx_channels).

I also did a talk on WebRTC with Django, Channels, and HTMX at this past DjangoCon - [https://youtu.be/8EJzcpw8i8s](https://youtu.be/8EJzcpw8i8s)  
This is a more comprehensive example of a real-life use of HTMX and Channels for displaying live information.

But you don’t _need_ to use HTMX for this. The key point of any of this is that it’s just data coming through the websocket, and it’s up to you to provide the JavaScript that knows what to do with it.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 21, 2025, 12:44pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/7 "2025-01-21T12:44:33Z")

</div>

Hi, Ken.

Ill check out the links. Im only looking to render text back to the screen it will be rended using bootstrap so it looks nice hopefully 🙂

Basically when someone adds a comment i want to render that in the terminal/screen so someone can view realtime.

The data is just a field in my model.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 23, 2025, 7:39am UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/8 "2025-01-23T07:39:22Z")

</div>

I’ve managed to get websockets running locally, which is really cool. But when i run this in production using https i get the error

`terminal.js:29 WebSocket closed unexpectedly`

Im not sure how to troubleshoot this?

I’m running with gunicorn, confused if I need to use Daphne?

I’m running this in a container in Kubernetes, if that makes any difference?

Thanks

Tom

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 23, 2025, 12:52pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/9 "2025-01-23T12:52:21Z")

</div>

> [@TommyQuality](#):
>
> I’m running with gunicorn, confused if I need to use Daphne?

You absolutely need to use Daphne (or some other async-based server such as uvicorn) with Channels listeners.

If you’re using Daphne in development as described in the docs, you’ll see that when you’re testing with `runserver`, it’s actually running an instance of Daphne.

Additionally, if you’re running this project behind nginx, you’ll want to split out the websocket url from the regular Django url so that you can apply different nginx directives for each kind on connection (websocket vs standard http).

Finally, what we do is run two different servers for the two types of connections. We run uwsgi for the regular Django work and Daphne for the websockets.

If you search through the forum, both this topic and deployment, you’ll find a number of threads discussing this.

Regarding Kubernetes, it shouldn’t matter. We do this in one project using Docker containers and it works great. I have no reason to believe that adding K8s into the mix fundamentally changes anything.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 23, 2025, 5:54pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/10 "2025-01-23T17:54:43Z")

</div>

So if I install channels[daphne] would that allow me to run both on the single server? Would I still need to create a separate route on my ingress controller or can I send both ws and http traffic on the same app, I’m assuming that when it makes the ws connection this is client side and therefore requires a separate route to wss/mywebsocketserver?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 23, 2025, 6:08pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/11 "2025-01-23T18:08:51Z")

</div>

> [@TommyQuality](#):
>
> So if I install channels[daphne] would that allow me to run both on the single server?

You can do it that way, yes. I prefer not to because we don’t use asynchronous views and consider the synchronous server (uwsgi) to be the safer and better-known choice.

> [@TommyQuality](#):
>
> Would I still need to create a separate route on my ingress controller

Yes.

Technically, they’re still fundamentally “HTTP” requests, but how they are handled internally are different.

> [@TommyQuality](#):
>
> I’m assuming that when it makes the ws connection this is client side and therefore requires a separate route to wss/mywebsocketserver?

Yes, it’s JavaScript that opens the websocket connection. (See [Writing WebSocket client applications - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications) for more details on this.)

Beyond that, I’m not quite sure what you’re asking here.

In the general case, you would use nginx (or Apache) to handle static and media files directly, route the websocket url to the Channels consumer, and everything else to your Django wsgi container. If you service them both using Daphne, you will _still_ want the separate paths defined in nginx, but also configuring a [Django Channels router](https://channels.readthedocs.io/en/latest/topics/routing.html).

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 23, 2025, 6:13pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/12 "2025-01-23T18:13:42Z")

</div>

Ok. So I am a bit confused on the ingress. I’m using azure application gateway as my ingress controller. It has native support for websockets

I have a route on my ingress ‘[https://mysite.com](https://mysite.com)’

What would the url be that I need to add for websockets.

As all traffic https should go to the app,

Or would I create a route wss://mysite.com. I think this is what I need to do?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 23, 2025, 6:27pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/13 "2025-01-23T18:27:50Z")

</div>

I’m sorry, I can’t help you here specifically.

In the abstract, you’ve got two different types of HTTP connections being made.  
One is what I’ll call standard HTTP, which is the traditional request followed by a traditional response. The other is the websocket upgrade request, which superficially “looks” like HTTP on the protocol layer, but needs to be handled differently by the backend.

How you set this up to be segregated and managed is up to you.

In my case, I use nginx to receive all requests. It proxies everything in the `/ws/` location to Daphne for a websocket. The `/static/` and `/media/` locations are handled directly. Everything else is proxied to uwsgi as a standard Django app.

I’m not sure I understand what you mean by:

> [@TommyQuality](#):
>
> Or would I create a route `wss://mysite.com`.

What “route” are you talking about?

Yes, in your JavaScript you generally reference it as `wss://mysite.com`. However, it’s still fundamentally an HTTP connection, and nginx sees it coming in as an HTTP request.  
Recognizing this, [WebSocket: WebSocket() constructor - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket#browser_compatibility) shows that the `url` parameter in the JavaScript `WebSocket()` constructor now allows `https`, `http`, and relative URLs to be used instead of the `wss://` or `ws://` forms.

For more details on this, you may also want to see [RFC 6455: The WebSocket Protocol](https://www.rfc-editor.org/rfc/rfc6455.html), particularly section 1.3, for details on the WebSocket connection request.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 23, 2025, 6:37pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/14 "2025-01-23T18:37:14Z")

</div>

Thanks again, Ken.

I’ve looked through the applications gateway docs for websockets and it’s says i create a route [ws.mysite.com](http://ws.mysite.com) point to my app. So I’m going to give this a go. Hopefully this is what is blocking the ws connection from establishing

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 23, 2025, 9:08pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/15 "2025-01-23T21:08:31Z")

</div>

Ok, so i was mistaken, I dont need to add an additonal route as i already have a route on my ingress for https traffic going to my django app.

So im not sure what is stopping the connection from being established.

Do you offload the certificate on nginx so that you hit the frontend on 443 and then pass the traffic to the app over 80?

Or do you have full ssl all the way?

I terminate the ssl on mine so i’m wondering if this is causing the problem?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 23, 2025, 9:40pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/16 "2025-01-23T21:40:17Z")

</div>

> [@TommyQuality](#):
>
> Do you offload the certificate on nginx so that you hit the frontend on 443 and then pass the traffic to the app over 80?

Yes. There are very few circumstances (actually, only 1 truly legitimate and 1 quasi-reasonable that I can think of off-hand) where it’s beneficitial to require uwsgi / Daphne / gunicorn / etc to handle the SSL layer. (There may be more than that, I’m just not immediately thinking or aware of more.)

> [@TommyQuality](#):
>
> I terminate the ssl on mine so i’m wondering if this is causing the problem?

It can be done - so doing it isn’t the issue. However, everything does need to be configured correctly - and when it’s not, there’s generally _some_ kind of error condition thrown.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 23, 2025, 9:44pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/17 "2025-01-23T21:44:08Z")

</div>

I’ve now deployed my app to standard webapp with no app gateway or ssl offload involved and it’s still not working, so i think this suggest it must be configuration somewhere i have missed.

Hard to know where because it works locally.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 23, 2025, 9:50pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/18 "2025-01-23T21:50:00Z")

</div>

is this correct:

```auto
application = ProtocolTypeRouter({
    'http': get_asgi_application(),
    'websocket': AuthMiddlewareStack(
        URLRouter(
            terminal.routing.websocket_urlpatterns
        )
    ),
})

```

Or should i be using `https: get_asgi_application()` when in production?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 23, 2025, 9:56pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/19 "2025-01-23T21:56:47Z")

</div>

The protocol is http.

But you really shouldn’t be looking at these separate pieces independently.

They are all interrelated, and every setting needs to be evaluated with respect to all the other settings. There are many different combinations of things that will work - assuming they’re all coordinated.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 23, 2025, 10:05pm UTC](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106/20 "2025-01-23T22:05:11Z")

</div>

Do i need to run a different startup command when running `Daphne` and `gunicorn`

```auto
gunicorn -w 2 -b 0.0.0.0:8000 my_app.wsgi:application

```

[Next page](https://forum.djangoproject.com/t/web-sockets-streaming-app/38106.md?page=2)
