简体   繁体   中英

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

I have a Python/Flask backend and an Angular frontend for my website. 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. 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. If you want to keep it in python/flask, there is this tutorial on how to do that with gevent:

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".

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.

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:

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. You can communicate with these services through your Python app (when the SQS notification happens) and they'll notify all connected clients for you.

I know this is a bit late, but I have done pretty much exactly what you ask for (though without 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

This allows you to send push notifications to your clients from pretty much anything that can access redis.

So when I want to publish a message to all my connected clients I just use redis like this:

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 :-)

You could of course use any websocket server or client (like socket.io or sock.js), but this has worked great for me :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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