I am setting up a dashboard which requires a Python Flask server to fetch public transport data once every few seconds or so and send this data to a client webpage through websockets. I am using FlaskSocketIO to achieve this.
I am able to send a message upon the 'connect' event, for example, but I've been unable to find a way to continuously update this message. This is my approach:
from flask import Flask, render_template, url_for, request
from flask_socketio import SocketIO, emit, send
import eventlet
eventlet.monkey_patch()
app = Flask(__name__)
socketio = SocketIO(app, logger=True, engineio_logger=True)
def listen():
while True:
message = # some updating message
emit('message', message)
socketio.sleep(1)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('connect')
def handle_connect():
listen()
if __name__ == "__main__":
socketio.run(app, debug=True)
The logger says it sends the message. However, on the client side, no message is recieved. The client side JavaScript is:
var socket = io();
socket.on('message', function(msg) {
console.log(msg);
});