Skip to content

Is there a way to catch socket.io events without JavaScript in Flask?

My application handels a countdown, which refreshes all the time. The countdown itself is handeld by the server, like so:

@socketio.on("update-countdown")
def update_countdown():
   # do some stuff here to gather countdown
   socketio.emit("update-countdown", countdown)

Currently I am catching the event data in JavaScript like this:

var socket = io.connect();

var countdown_wrapper = document.getElementById("countdown");
socket.on("update-countdown", function(countdown) {
   countdown_wrapper.innerHTML = countdown;
});

Q.: Is there a way to catch socket.io events without JavaScript? If so, how could I do this?

Answer

If you are looking to replace javascript with python within your web browser’s client-side code, you might want to look into https://brython.info/

It is essentially just using python within the browser. You wouldn’t have all of those fancy decorators, but it would probably work well enough.