What is the best way to update all clients when Fl

2020-05-07 09:35发布

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条回答
地球回转人心会变
2楼-- · 2020-05-07 09:55

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.

查看更多
登录 后发表回答