sseclient icon indicating copy to clipboard operation
sseclient copied to clipboard

Handling redirects, add origin to Event

Open TheSandDoctor opened this issue 9 months ago • 1 comments

Code comment:

python
# TODO: Ensure we're handling redirects.  Might also stick the 'origin'
        # attribute on Events like the Javascript spec requires.

We should ensure that's implemented.

TheSandDoctor avatar Jun 05 '25 13:06 TheSandDoctor

Redirects are already implicitly handled, but we can improve upon that to make it explicitly clear. Test case demonstrating this below.

server.py:

from flask import Flask, redirect, Response
import time
app = Flask(__name__)

@app.route('/old-stream')
def old():
    return redirect('/new-stream', code=301)

@app.route('/new-stream')
def new_stream():
    def gen():
        yield "data: hello\n\n"
        while True:
            time.sleep(1)
            yield "data: ping\n\n"
    return Response(gen(), mimetype='text/event-stream')

if __name__ == "__main__":
    app.run(port=5050)

client.py:

from sseclient import SSEClient

messages = SSEClient('http://localhost:5050/old-stream')
for msg in messages:
    print(msg)

output:

python switch-client.py
hello
ping
ping

TheSandDoctor avatar Jun 05 '25 13:06 TheSandDoctor