Push notifications from a Flask application

Viewed 2317

I am sure this has been covered in some other question on SO but I am unable to recognize it. My problem is simple. I have a Flask REST app where an external party can post some information to one end point (say /post_event). I then want to be able to publish this received information to refresh a browser page automatically. Assume there is a browser window open to the same Flask app home page which I want to refresh. How do I do this? I tried websockets but couldn't see how I can get it to work. What other technologies can I use to achieve this?

1 Answers

It would help if you had asynchronous techniques to refresh your clients.

The first option is WebSocket, but you should try something like socket.io on Express and write a service for your updating functionality. It's like a separate service that relays messages to your clients, but your REST API is on the flask.

The second option is using server-sent events; You can connect with the server, and the server sends clients all the updates. I found this module for flask: flask-sse

There are also some frameworks like FastAPI that provide concurrency; Of course, you need to read more about this framework to see if it can help with your problem or not. There are some techniques like long-polling that you can implement with this framework.

In the end, I had the same situation in my company, and we decided not to use python for our live updating things because the express framework with the socket.io package has a lot of features that can solve anything you want in action.

Related