Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
from flask import Flask, Response
from flask_cors import CORS
import requests

app = Flask(__name__)
CORS(app)

@app.route('/deals')
def forwardStream():
    r = requests.get('http://localhost:8080/streampath', stream=True)
    def eventStream():
            for line in r.iter_lines( chunk_size=1):
                if line:
					# the next lines emit the received data as a SSE
                    yield 'data:{}\n\n'.format(line.decode())
    return Response(eventStream(), mimetype="text/event-stream")

Useful websites

https://medium.com/code-zen/python-generator-and-html-server-sent-events-3cdf14140e56

...