How do you send messages from Flask server (Python) to HTML client? How do you send messages from Flask server (Python) to HTML client? flask flask

How do you send messages from Flask server (Python) to HTML client?


Here's a simple example showing how to connect to the SocketIO server and receive a message from the server upon that connection.

app.py

from flask import Flask, render_template                                        from flask_socketio import SocketIO, emit                                       app = Flask(__name__)                                                           app.config['SECRET_KEY'] = 'secret!'                                            socketio = SocketIO(app)                                                        @socketio.on('connect')                                                         def connect():                                                                      emit('message', {'hello': "Hello"})                                         @app.route('/')                                                                 def index():                                                                        return render_template('index.html')                                        if __name__ == '__main__':                                                          socketio.run(app, debug=True) 

templates/index.html

<head>      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>      <script type="text/javascript" charset="utf-8">            var socket = io.connect('http://' + document.domain + ':' + location.port);            socket.on('connect', function() {                 console.log('connected');            });            socket.on('message', function(data) {                 console.log(data);            });      </script></head>

Edit:

To have the server send a socketio message every five seconds, you can use a background thread:

app.py

from flask import Flask, render_template                                        from flask_socketio import SocketIO, emit                                       import time                                                                     app = Flask(__name__)                                                           app.config['SECRET_KEY'] = 'secret!'                                            socketio = SocketIO(app)                                                        thread = None                                                                   def background_thread():                                                            while True:                                                                         socketio.emit('message', {'goodbye': "Goodbye"})                                time.sleep(5)                                                           @socketio.on('connect')                                                         def connect():                                                                      global thread                                                                   if thread is None:                                                                  thread = socketio.start_background_task(target=background_thread)       @app.route('/')                                                                 def index():                                                                        return render_template('index.html')        if __name__ == '__main__':                                                          socketio.run(app, debug=True)