6

I am playing around with a small project to get a better understanding of web technologies. One requirement is that if multiple clients have access to my site, and one make a change all the others should be notified. From what I have gathered Server Sent events seems to do what I want. However when I open my site in both Firefox and Chrome and try to send an event, only one of the browsers gets it. If I send an event again only one of the browsers gets the new event, usually the browser that did not get event number one.

Here is the relevant code snippets.

Client:

console.log("setting sse handlers")
viewEventSource = new EventSource("{{ url_for('viewEventRequest') }}");
viewEventSource.onmessage = handleViewEvent;

function handleViewEvent(event){
    console.log("called handle view event")
    console.log(event);
}

Server:

@app.route('/3-3-3/view-event')
def view_event_request():
    return Response(handle_view_event(), mimetype='text/event-stream')

def handle_view_event():
while True:
    for message in pubsub_view.listen():
        if message['type'] == 'message':
            data = 'retry: 1\n'
            data += 'data: ' + message['data'] + '\n\n'
            return data

@app.route('/3-3-3/test')
def test():
    red.publish('view-event', "This is a test message")
    return make_response("success", 200)

My question is, how do I get the event send to all connected clients and not just one?

2
  • I am unfamiliar with server sent events so I do not know the issue here, but I'd recommend researching other solutions like WebSockets and socket.io to see if they fit your case better. Commented Jan 5, 2013 at 22:01
  • Websockets uses full-duplex communications between client-server. But the issue is just about send some events to multiple clients. So, Anorov your recommendation is not proper. Commented Mar 11, 2021 at 7:34

1 Answer 1

1

Here are some gists that may help (I've been meaning to release something like 'flask-sse' based on 'django-sse):

https://gist.github.com/3680055

https://gist.github.com/3687523

also useful - https://github.com/jkbr/chat/blob/master/app.py

The first thing I notice about your code is that 'handle_view_event' is not a generator. Though it is in a 'while' loop, the use of 'return' will always exit the function the first time we return data; a function can only return once. I think you want it to be 'yield' instead.

In any case, the above links should give you an example of a working setup.

As Anarov says, websockets and socket.io are also an option, but SSE should work anyway. I think socket.io supports using SSE if ws are not needed.

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

5 Comments

Thanks for the answer, however I already have SSE working my problem is that if I send an event not all the clients will get it. The same happens with your code, if I run it in two browsers one of them will not load, if I used run(threaded=True) then it will, but the browsers do not get the same values. I suspect this might be due to the server running multiples threads.
The values still only show up in one of the tabs or browsers if multiple is connected. However I have changed the requirement for the server so it no longer needs to push notifications. Thank you for your answer.
Even with gevent and gunicorn? Strange, I'm not sure why. Thanks.
This also does not work for me, I am having the same problem.
I have the same issue that my event stream from Flask only shows in one tab in chrome. I used threaded=True but not working. Did you guys find the root cause and how to solve this?

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.