3

I have flask-socketio as backend and flutter as frontend. I am using flutter socket-io-client to connect to flask-socektio. It connects but I am receiving these continous messages on flask side

Sending packet PING data None 
Received packet PONG data None
Sending packet PING data None 
Received packet PONG data None
Sending packet PING data None 
Received packet PONG data None
Sending packet PING data None 
Received packet PONG data None ...... and so on

I wanted to get rid of this. How do I do it? also when I am sending/ emiting any data from frontend, the

socket.on('event' , (data)=>print(data) method prints null. So how do I send data from socketio event from backend to frontend? also I do I stop getting those messaged on PING PONG on server side?

Here is my flask code

from flask import Flask, request, jsonify
from flask_socketio import SocketIO

app = Flask(__name__)
app.secret_key = 'secret'

socketio = SocketIO(app,cors_allowed_origins="*")

app.debug = True


@app.route('/', methods=['GET'])
def index():
    return 'Welcome!'

@socketio.on('connect')
def test_connect():
    print('Client Connected')

@socketio.on('disconnect')
def test_disconnect():
    print('Client disconnected')

@socketio.on('test')
def test():
    print('Testing')
    // i want to return some data to frontend here, how do I do it?

if __name__ == '__main__':
    socketio.run(app,debug=True,host='0.0.0.0',port=5000)

Here is my flutter socket code

//using socket-io-client

IO.Socket socket = IO.io('http://192.16x.x.x:3000/', <String, dynamic>{
      'transports': ['websocket']
});

socket.onConnect((_) {
   print('connect');
});

scoket.on('test',(data)=>print(data)); //printing here the data received from backend

socket.onDisconnect((_) => print('disconnect'));

1 Answers1

0

I have the similar problem in FastAPI (another Python framework similar with Flask).

Found a perfect solution here by using loguru.

This solution is for uvicorn web server, may need some change to suit Flask one

R.Liu
  • 361
  • 3
  • 6