title: How to send an alert to client using websockets after post request in flask
body: <p>How do I send a message to client after a post request is made? I store the data from the post request into a database and I want to notify the client that the database has been changed. Is there way to combine the restapi with websocket? the examples I am finding are using websockets to recieve data and send to client while I am using post method receive and want to use websocket to alert. any help would be appreciated. </p>
  
  <p>server.py</p>
  
  <pre><code>import sqlite3
  from flask import Flask, render_template
  from flask_socketio import SocketIO
  
  app = Flask(__name__)
  app.config['SECRET_KEY'] = 'secret!'
  socketio = SocketIO(app)
  
  @app.route('/', methods=['GET'])
  def index():
      render_template('index.html')
  
  # listen for data
  @app.route('/listen/&lt;data&gt;',methods=['POST'])
  def listen(data):
      # insert to db
      conn = sqlite3.connect('storage.db')
      c = conn.cursor()
      c.execute('''CREATE TABLE info(num text)''')
      c.execute('''INSERT INTO info(num) VALUES(?)''',(data))
      conn.commit()
      conn.close()
  
      #send alert to client
  
  @socketio.on('message')
  def handle_message(message):
      send(message)
  
  if __name__ == '__main__':
      socketio.run(app)
  </code></pre>
  
  <p>index.html</p>
  
  <pre><code>&lt;head&gt;
  &lt;script type="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" charset="utf-8"&gt;
          var socket = io.connect('http://' + document.domain + ':' + location.port);
          socket.on('message', function(data) {
               console.log(data);
          });
    &lt;/script&gt;
  &lt;/head&gt;
  </code></pre>