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

前端 未结 1 1424
栀梦
栀梦 2021-01-27 10:17

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:



        
相关标签:
1条回答
  • 2021-01-27 11:00

    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.

    0 讨论(0)
提交回复
热议问题