Can't establish connection with React-Native
I tried ti implement socket connection between react native and flask,for client side i use socket.io-client npm package
import io from 'socket.io-client/dist/socket.io';
class App extends Component {
constructor(props){
super(props);
this.socket = io("http://10.0.2.2:5000")
this.socket.on("retrieve_active_users", () => {
this.socket.emit("activate_user", { username: "test_user" })
})
this.socket.on('user_activated', (data) => {
console.log(data)
})
}
from flask_socketio import SocketIO, join_room, leave_room, emit
app = Flask(__name__)
Debug(app)
cors = CORS(app,resources={r"/api/*":{"origins":"*"}})
socketio = SocketIO(app,engineio_logger=True,logger=True)
@socketio.on('connect')
def connect():
print('listening to connect')
retrieve_active_users()
def retrieve_active_users():
print("trigger")
emit('retrieve_active_users')
@socketio.on('activate_user')
def on_active_user(data):
user = data.get('username')
emit('user_activated', {'user': user}, broadcast=True)
if __name__ == '__main__':
socketio.run(app, debug=True, host='127.0.0.1')
output
this.socket.on('user_activated', (data) => {
console.log(data)
})
listening to connect // print statements that i have added
trigger // print statements that i have added
"GET /socket.io/?EIO=3&transport=polling&t=N04fglI HTTP/1.1" 200
Invalid session None
"POST /socket.io/?EIO=3&transport=polling&t=N0C2eN_ HTTP/1.1" 400
b05af575be8045928c73c97c1c79e123: Client is gone, closing socket
Your client is sending requests that do not include the session. Note the Invalid session None message. This issue may have a workaround for your problem: https://github.com/miguelgrinberg/Flask-SocketIO/issues/716.
Thanks for your quick reply, as it mentioned i changed this.socket = io(http://10.0.2.2:5000, { transports: ['websocket']});
now my output shows as below
still i'm getting 400 error
so i add below lines in my client side
this.socket.on('connect_error', (error) => {
console.log(error)
});
it shows
please guide me to fix this and if this issue is not related to #####Flask-SocketIO close the issue
Most helpful comment
Thanks for your quick reply, as it mentioned i changed

this.socket = io(http://10.0.2.2:5000, { transports: ['websocket']});now my output shows as below
still i'm getting 400 error
so i add below lines in my client side
this.socket.on('connect_error', (error) => { console.log(error) });it shows

please guide me to fix this and if this issue is not related to #####Flask-SocketIO close the issue