Non-terminating HttpResponse

I am thinking of building a little notification box with real-time notification. I used Channels before but for such a little thing it seems overkill. I am looking for a super-simple solution (like HTTP event-source). Here’s the idea:

import asyncio

async def clock(request):
    return HttpResponse(terminator=clock_loop)

async def clock_loop(send, end):
    for _ in range(5):
        asyncio.sleep(1)
        await send("hello")

I don’t think this is possible at the moment, not even with StreamingResponse. Is it?

What on the browser end is going to be receiving this?

You might want to verify that the browser is going to work with what you’re trying, by implementing this in the most trivial way possible on the server side (as a synchronous server), and see if the browsers are going to function properly.

I should have been a bit more explicit. I want to use this to generate Server sent events, which is very well supported on many browsers, with not much JS needed.

On the python side, it could be easy to generate. It’s just a bunch of lines starting with the string “event:”. I had a look at the Django source code, and it seems to me it would require a lot of changes to the core handlers.

I am interested to see if there’s someone that has a simple solution to this, something that is eluding me at the moment…

Interesting information, thanks! No, I don’t have a simple solution for this - but then the sites I work on don’t get so loaded to the point where a simple synchronous solution won’t work.

(Last time I had to work on something like this, we used a long-polling mechanism for streaming data.)

Ken