Flask: Adding documentation on best practices for Server-sent events and WebSockets

Created on 3 Nov 2016  路  4Comments  路  Source: pallets/flask

Earlier this year I did some development involving Server-sent events (SSE) using Flask. I found there is no official documentation on how to best handle this. Since the specification is simple I ended up implementing it myself. However, I think there is room for improvement.

I find there are a couple of problems with working with SSE and Flask:

  • Development environment requires using a separate service or threading. Using a separate service adds complexity and using threading disallows the use of frame inspection in the built-in Don't Panic debugger for all requests.
  • There is no official information on best practices when it comes to deployment of SSE. Should it be a separate service or can it be included in the application? Especially for smaller applications I find the complexity of introducing a separate service for managing SSE adds a lot of complexity to an otherwise simple project.

The same points hold true for WebSockets as they are similar.

Most helpful comment

I'd argue that it would be useful to have a section for this under _Patterns for Flask_ is useful and relevant. There already are patterns for specific technologies such as _Celery Based Background Tasks_. I know about the Flask extensions, but they all introduce problems:

  • You can no longer run your app using flask run
  • The Werkzeug debugger no longer works

The thing about the linked talk I find useful is that @mitsuhiko advocates for not using Flask for the actual websocket server. Stating that _"Flask doesn't do websockets and SSE very well yet, and therefore you should implement it as a separate service. You should do this using signed tokens."_ is useful to everyone looking at the documentation.

Since both SSE and websockets are becoming increasingly popular, I really think they should be addressed in some way in the documentation. It doesn't even have to be best practices, just give the user a good starting point.

All 4 comments

@mitsuhiko did a talk at PyBay 2016 about Flask, and one of the last sections was on WebSockets with Flask. I'm linking to the part about WebSockets here for future reference:

https://youtu.be/1ByQhAM5c1I?t=2493

Flask is not going to define "best practices" for most things, as that's up to the developer. There is Flask-SSE for SSE, Flask-Sockets or Flask-SocketIO for websockets.

The takeaway from the talk you linked is that Flask itself doesn't do anything, it's a separate service, which the extensions above cover. If you're interested in adding async support to Flask itself, I'm investigating ASGI.

I'd argue that it would be useful to have a section for this under _Patterns for Flask_ is useful and relevant. There already are patterns for specific technologies such as _Celery Based Background Tasks_. I know about the Flask extensions, but they all introduce problems:

  • You can no longer run your app using flask run
  • The Werkzeug debugger no longer works

The thing about the linked talk I find useful is that @mitsuhiko advocates for not using Flask for the actual websocket server. Stating that _"Flask doesn't do websockets and SSE very well yet, and therefore you should implement it as a separate service. You should do this using signed tokens."_ is useful to everyone looking at the documentation.

Since both SSE and websockets are becoming increasingly popular, I really think they should be addressed in some way in the documentation. It doesn't even have to be best practices, just give the user a good starting point.

I vote for adding documentation about SSE topic. Because some points of implementation of SSE on flask are not clear at all. For example I'm getting resource leaking and don't know how to avoid it https://stackoverflow.com/questions/47868205/detect-client-disconnect-in-flask-for-streaming-response-server-sent-events-s.
Or if flask team agrees that flask is not good for SSE and other async services it would be great to explicit note it in documentation as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rkomorn picture rkomorn  路  3Comments

KeyC0de picture KeyC0de  路  4Comments

greyli picture greyli  路  3Comments

xliiv picture xliiv  路  3Comments

sungjinp11 picture sungjinp11  路  3Comments