2

We currently have a django app serving our frontend website to the users over Apache. The frontend queries our backend, which is a separate Python app (Flask) served a different server. So far, the frontend has been required to query the backend for data but not the other way around.

We are now introducing data alerts that users can choose to receive regarding data changes in the system. The process for checking whether an alert has happened has to occur on the backend, as it is closely coupled with the data model. Hence, the frontend sends a request to add an alert in the backend, which will then poll and check if the triggers have occurred.

The issue is that once alerts are triggered in the backend, the frontend needs to get notified so that users can be notified as well. The backend cannot directly alert the users since it is unaware of user data like emails etc. (this user data might change, hence sending it to the backend alongside the request to add the alert is too simplistic).

Hence, we are looking for the optimal way to push alert notifications from the backend to the frontend. What is our best approach at doing this? We can set up a view on the django app that the backend can query, but I don't like the idea of the frontend querying the backend as well as the other way around. What is our best bet at achieving this?

user1094786
  • 6,402
  • 7
  • 29
  • 42

1 Answers1

5

For an overview of some available methods: What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?.

Since you need to be able to communicate updates to the front end without having the client constantly poll, it sounds like you will need to setup web sockets or server sent events, and for legacy systems long polling or other methods (maybe try a service like pusher.io if necessary for scaling these things). There are a number of great tutorials about integrating these items into a flask or django setup. Here are some great tutorials: http://blog.jupo.org/2011/08/13/real-time-web-apps-with-django-and-websockets/ and http://dev.hasenj.org/post/38188152502.

In order to get a minimal solution up and running, I'd recommend giving socket.io a look (http://socket.io/), and then use a python library with it (Socket.IO Client Library in Python).

If you need authentication, when a user logs in, have the frontend send a request for a new websockets channel name, and then keep that channel open until they logout or their cookies expire. Use this channel to send the user updates.

Community
  • 1
  • 1
Anil Vaitla
  • 2,958
  • 22
  • 31