I have a Flask server with a webhook endpoint for Meraki Alerts ("host_url/endpoint").
The server also has a page which is supposed to display all recieved alerts ("host_url/view").
What I would love to happen is that when Meraki sends a webhook to my endpoint all the clients on "host_url/view" update to show the latest alerts.
I have tried simply polling every few seconds, but even though it works I would like to avoid this solution.
I also tried using websockets, but the standard flask_socketio implementation requires messages send from the server to be within functions decorated with an @socketio.on("...") decorator.
E.g.
@socketio.on("connect")
def send_message():
emit("msg")
But since the event that triggers such a message isn't a websocket event in itself I need to be able to send messages outside of a socketio function.
E.g.
def send_message():
emit("msg")
My goal is to have a function send_to_all_clients(data) that I can use from everywhere in my code and that sends an event that can be recieved by a Javascript event handler.
To send an event to all clients, I recommend taking a look at the “Broadcasting” section of the Flask-SocketIO documentation. Here it is described how you can use
socketio.emit(eventName, data)to send an event, for example from a normal route, so that it can be received by all connected clients.In the following example, at the push of a button an example event is sent to the webhook, which is then passed on to all clients. Using Postman would certainly be more advisable.