简体   繁体   English

可以使用AngularJS + Flask堆栈推送通知吗?

[英]Can push notifications be done with an AngularJS+Flask stack?

I have a Python/Flask backend and an Angular frontend for my website. 我的网站有一个Python / Flask后端和一个Angular前端。 At the backend there is a process that occasionally checks SQS for messages and I want it to push a notification to the client which can then in turn update an Angular controller. 在后端有一个进程偶尔检查SQS的消息,我希望它向客户端发送通知,然后客户端可以更新Angular控制器。 What is the best way to do this my existing technologies? 我现有技术的最佳方法是什么?

To be able to push to the client, you'll have to implement web socket support in some fashion. 为了能够推送到客户端,您必须以某种方式实现Web套接字支持。 If you want to keep it in python/flask, there is this tutorial on how to do that with gevent: 如果你想将它保存在python / flask中,那么有关于如何使用gevent执行此操作的教程:

http://www.socketubs.org/2012/10/28/Websocket_with_flask_and_gevent.html http://www.socketubs.org/2012/10/28/Websocket_with_flask_and_gevent.html

In that article, Geoffrey also mentions a SocketIO compatible library for python/gevent that may allow you to leverage the SocketIO client-side JS library, called "gevent-socketio". 在那篇文章中,Geoffrey还提到了一个用于python / gevent的SocketIO兼容库,它可以让你利用名为“gevent-socketio”的SocketIO客户端JS库。

That may reduce how much work you have to do in terms of cross-browser compatibility since SocketIO has done a lot of that already. 这可能会减少您在跨浏览器兼容性方面需要做多少工作,因为SocketIO已经完成了很多工作。

Here is a pretty good tutorial on how to use SocketIO in AngularJS so that you can notify the AngularJS model when an event comes in from SocketIO: 这是一个非常好的教程,介绍如何在AngularJS中使用SocketIO,以便在SocketIO发生事件时通知AngularJS模型:

http://www.html5rocks.com/en/tutorials/frameworks/angular-websockets/ http://www.html5rocks.com/en/tutorials/frameworks/angular-websockets/

If you don't want to host the web socket backend, you could look to a hosted service like PubNub or Pusher and then integrate them into AngularJS as a service. 如果您不想托管Web套接字后端,可以查看PubNub或Pusher等托管服务,然后将它们作为服务集成到AngularJS中。 You can communicate with these services through your Python app (when the SQS notification happens) and they'll notify all connected clients for you. 您可以通过Python应用程序与这些服务进行通信(当SQS通知发生时),他们将为您通知所有连接的客户端。

I know this is a bit late, but I have done pretty much exactly what you ask for (though without Angular). 我知道这有点晚了,但我已经完全按照你的要求做了(尽管没有Angular)。

I ended up having a separate process running a websocket server called Autobahn , which listens to a redis pub/sub socket, the code is here on github 我最终有一个单独的进程运行一个名为Autobahn的websocket服务器,该服务器监听一个redis pub / sub套接字,代码在github上

This allows you to send push notifications to your clients from pretty much anything that can access redis. 这允许您从几乎任何可以访问redis的东西向您的客户端发送推送通知。

So when I want to publish a message to all my connected clients I just use redis like this: 因此,当我想向所有连接的客户端发布消息时,我只需使用redis:

r = redis.Redis()
r.publish('broadcasts', "SOME MESSAGE")

This has worked fairly good so far. 到目前为止,这已经相当不错了。 What I can't currently do is send a push notification to a specific client. 我目前无法做的是向特定客户发送推送通知。 But if you have a authentication system or something to identify a specific user you could tie that to the open websockets and then be able to send messages directly to a specific client :-) 但是,如果您有一个身份验证系统或某些东西来识别特定用户,您可以将其绑定到打开的websockets,然后能够直接向特定客户端发送消息:-)

You could of course use any websocket server or client (like socket.io or sock.js), but this has worked great for me :-) 你当然可以使用任何websocket服务器或客户端(如socket.io或sock.js),但这对我来说很有用:-)

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

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