简体   繁体   English

来自 Flask 应用程序的推送通知

[英]Push notifications from a Flask application

I am sure this has been covered in some other question on SO but I am unable to recognize it.我确信这已经在关于 SO 的其他一些问题中得到了解决,但我无法识别它。 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).我有一个 Flask REST 应用程序,外部方可以将一些信息发布到一个端点(例如 /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.假设有一个浏览器 window 打开到我想刷新的同一个 Flask 应用程序主页。 How do I do this?我该怎么做呢? I tried websockets but couldn't see how I can get it to work.我尝试了 websockets,但看不到如何让它工作。 What other technologies can I use to achieve this?我可以使用哪些其他技术来实现这一目标?

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.第一个选项是 WebSocket,但您应该在Express上尝试类似socket.io的方法,并为您的更新功能编写服务。 It's like a separate service that relays messages to your clients, but your REST API is on the flask.它就像一个单独的服务,将消息中继到您的客户端,但您的REST API 在 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我为flask找到了这个模块: flask-sse

There are also some frameworks like FastAPI that provide concurrency;还有一些框架,如FastAPI提供并发; 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.您可以使用此框架实现一些技术,例如long-polling

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.最后,我的公司也遇到了同样的情况,我们决定不使用 python 来进行实时更新,因为带有socket.io package 的express框架有很多功能可以解决您想要的任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM