Moyote
Moyote

Reputation: 1217

Flask-SocketIO handshake fails

I'm new with Flask-SocketIO and I'm struggling to set up Socket IO connection between my Flask server and my Javascript (React) client.

Anyhow, I was not able to set up the connection and start to emit messages. This is what log says:

127.0.0.1 - - [20/Aug/2018 15:33:03] "GET /socket.io/?EIO=3&transport=polling&t=MLNKwf9 HTTP/1.1" 200 381 0.000659
127.0.0.1 - - [20/Aug/2018 15:33:03] "POST /socket.io/?EIO=3&transport=polling&t=MLNKwfK HTTP/1.1" 400 218 0.000203

These log messages recur on every 5 seconds.

Here's my code in back-end:

app = Flask(__name__, static_url_path=None)    
socket_io = SocketIO(app)

@socket_io.on('connect', namespace='/chat')
def test_connect():
    print('connected')

And in client side:

import SocketIOClient from 'socket.io-client';

and in constructor I do this:

const uri = `http://${localhost}:5000/api/v1.0/chat`;
this.socket = SocketIOClient(uri);

What do I wrong?

Any help would be appreciated!

Upvotes: 0

Views: 1040

Answers (1)

Moyote
Moyote

Reputation: 1217

In case anyone faces the similar problem, I was able to resolve this by myself.

Problem was that since my client is React Native app, I needed to modify my client code like this:

window.navigator.userAgent = 'ReactNative';

const io = require('socket.io-client/dist/socket.io');
const connectionConfig = {
  jsonp: false,
  reconnection: true,
  reconnectionDelay: 100,
  reconnectionAttempts: 100000,
  transports: ['websocket'], // you need to explicitly tell it to use websockets
};
const socket = io(path, connectionConfig);

This thread provided the answer: https://gist.github.com/ekryski/59eb6ce5b2774fa24d15

Upvotes: 1

Related Questions