Skip to content

A sample of a django app sending a StreamingHttpResponse and handling that stream in JS

License

Notifications You must be signed in to change notification settings

Lucacion/StreamingHttpResponse

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StreamingHttpResponse

A sample of a django app sending a StreamingHttpResponse and handling that stream in JS

We don't need really db or models in this example so the code is under apps/stream/views and apps/stream/templates/stream/index.html

How to use it

You can look for 'start new chunck:' so you can see that data comes in several chunks

Here is the view code.

We are using an iterator so you can replace it with your own long processing iterator

from django.http.response import StreamingHttpResponse
from django.shortcuts import render


def gen_message(msg):
    return 'data: {}'.format(msg)


def iterator():
    for i in range(10000):
        yield gen_message('iteration ' + str(i))


def test_stream(request):
    stream = iterator()
    response = StreamingHttpResponse(stream, status=200, content_type='text/event-stream')
    response['Cache-Control'] = 'no-cache'
    return response


def home(request):
    return render(request, 'stream/index.html')

About

A sample of a django app sending a StreamingHttpResponse and handling that stream in JS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 75.9%
  • HTML 24.1%