0

I found this example on the Bottle docs re asynchronous responses.

Here is the code slightly modified:

from gevent import monkey; monkey.patch_all()

from time import sleep
from bottle import hook, response, route, run


@route('/stream')
def stream():
    yield 'START'
    sleep(3)
    yield '\nPROGRESS 1'
    sleep(3)
    yield '\nPROGRESS 2'
    sleep(3)
    yield '\nPROGRESS 3'
    sleep(3)
    yield '\nEND'

run(host='0.0.0.0', port=8080, server='gevent')

calling this view from the browser works as explained in the docs:

If you run this script and point your browser to http://localhost:8080/stream, you should see START, MIDDLE, and END show up one by one (rather than waiting 8 seconds to see them all at once).

I was wondering how to handle this via Javascript in an AJAX request, specifically with JQuery, in order to display the messages in sequence.

ANy help about that?

1 Answer 1

1

jQuery doesn't support that, but you can do that with plain XHR:

var xhr = new XMLHttpRequest()
xhr.open("GET", "/test/chunked", true)
xhr.onprogress = function () {
  console.log("PROGRESS:", xhr.responseText)
}
xhr.send()

This works in all modern browsers, including IE 10. W3C specification here.

The downside here is that xhr.responseText contains an accumulated response. You can use substring on it, but a better idea is to use the responseType attribute and use slice on an ArrayBuffer.

SOURCE: How to write javascript in client side to receive and parse `chunked` response in time?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.