title: How to return the json object in the flask server side from socket client using python?
body: <p>On my client side, I have dataJson, and I want to sent this to server through socketio. On the Server side, I want this dataJson to be received and returned under the app.route in order to display it in the web.</p>
  
  <p>Code on the server side:</p>
  
  <pre><code>from flask import Flask, render_template
  from flask_socketio import SocketIO
  
  app = Flask(__name__)
  app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
  socketio = SocketIO(app)
  
  @app.route('/')
  def sessions():
      print(dataJson)
      return dataJson
  
  @socketio.on('my event')
  def handle_my_custom_event(json):
      global dataJson
      dataJson = json
      print('received json: ' + str(json))
  
  if __name__ == '__main__':
     socketio.run(app, debug=True, host='127.0.0.1')
  </code></pre>
  
  <p>Code on the client side:</p>
  
  <pre><code>from socketIO_client import SocketIO
  
  import json
  dataJson = {'xxx': 'yyy'}
  with SocketIO('localhost', 5000) as socketIO:
      json = json.dumps(dataJson)
      socketIO.emit(json)
      print(json)
  </code></pre>
  
  <p>Or is there any other method to achieve my goal?
  Thanks!</p>
  
  <p>I solve this problem.</p>
  
  <pre><code>Code on the server side
  from flask import Flask, render_template
  from flask_socketio import SocketIO
  import json
  app = Flask(__name__)
  app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
  socketio = SocketIO(app)
  
  dataJson = None
  
  @socketio.on('my event')
  def handle_my_custom_event(json):
      global dataJson
      dataJson = json
      print('received json: ' + str(json))
  
  @socketio.on('my event')
  def handle_my_custom_event(json):
      global dataJson
      dataJson = json
      print('received json: ' + str(json))
  
  @app.route('/')
  def sessions():
      global  dataJson
      print(dataJson)
      print(type(dataJson))
      Data = dataJson.decode('utf-8')
      JsonData = json.loads(Data)
      print(JsonData)
      print(type(JsonData))
  
      return JsonData
  
  if __name__ == '__main__':
     socketio.run(app, debug=True, host='127.0.0.1')
  </code></pre>
  
  <p>Code on the client side</p>
  
  <pre><code>import json
  import socketio
  
  
  
  sio = socketio.Client()
  
  def send_ping():
      while True:
        x = 1
        x=x+1
        jsonData = {'aaa': x}
        Data = json.dumps(jsonData).encode('utf-8')
        print(type(Data))
        sio.emit('my event', Data)
  
  @sio.event
  
  def connect():
  
      print('connected to server')
  
      send_ping()
  
  if __name__ == '__main__':
  
      sio.connect('http://localhost:5000')
  </code></pre>