What is the best way to update all clients when Flask database changes without polling?

◇◆丶佛笑我妖孽 提交于 2020-01-22 03:32:28

问题


Currently I have a Flask server that runs a small web frontend as well as a command line interface to that same server. The basic idea looks like this:

<Top section: allows file upload>
   * list of files from database

<Second section: allows file manipulation/ upload>
  * list of files  from database

<Third section: Not files, but still allows for database changes>
  * list of things made from database  

Now this works well from the front end, but currently if the CLI or another client makes a change to the database, it doesn't update other clients. I have it somewhat working with JS polling and rewriting the list of files every 10s, but that seems both inefficient and also would look very messy if I had to do it for every section. I saw websockets mentioned in various forums, but I've never used them and am unsure if it would be a pain to add. I'm not trying to rewrite the whole thing for a single feature.

Final takeaway: How to update all clients better than polling/ how to do polling efficiently?


回答1:


Yes you are correct. You need sockets. There are bunch of articles over the internet but I would like to give a summary and try to explain why sockets will be the best fit to your requirements.

Sockets are way of achieving two way communication between client and server without the need of polling.

There is a package called Flask-SocketIO

Flask-SocketIO gives Flask applications access to low latency bi-directional communications between the clients and the server.

Then for the scenario where you would like to send changes to all the connected client when one client does some work to your database or something similar, you will need to use broadcasting. When a message is sent with the broadcast option enabled, all clients connected to the namespace receive it, including the sender. Here you can find details of the broadcasting using Flask-SocketIO.



来源:https://stackoverflow.com/questions/45486960/what-is-the-best-way-to-update-all-clients-when-flask-database-changes-without-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!