Flask-socketio: Implementation with React-Native 0.61.5

Created on 3 Feb 2020  路  2Comments  路  Source: miguelgrinberg/Flask-SocketIO

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

client code
    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)
      })
    }
server side
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

these lines are not execute in client side
this.socket.on('user_activated', (data) => {    
    console.log(data)
})
server side

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

and some times

b05af575be8045928c73c97c1c79e123: Client is gone, closing socket

question

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
issue

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
issue2

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

All 2 comments

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
issue

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
issue2

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lnunno picture lnunno  路  4Comments

hrmon picture hrmon  路  5Comments

nh916 picture nh916  路  3Comments

thehelpfulbees picture thehelpfulbees  路  4Comments

novice79 picture novice79  路  3Comments