Flutter SocketIO client does not connect to python SocketIO server

923 Views Asked by At

I have set up a Python SocketIOServer, shown below:

import eventlet
import socketio

sio = socketio.Server()
app = socketio.WSGIApp(sio, static_files={
    '/': {'content_type': 'text/html', 'filename': 'index.html'}
})


@sio.event
def connect(sid, environ):
    print('connect ', sid)


@sio.event
def message(sid, data):
    print('message ', data)
    sio.send(data=f"im echoing :{data}", to=sid)


@sio.event
def disconnect(sid):
    print('disconnect ', sid)


if __name__ == '__main__':
    eventlet.wsgi.server(eventlet.listen(('', 5000)), app)

I tested it with another python socketio client and it worked, the onConnect handler was called and a reply was emitted.

I used socket io dart package to connect to my python server, but I can't make it work. The code in my flutter app:

@override
void initState() {
socket = IO.io('http://192.168.90.231:5000', <String, dynamic>{
  'transports': ['websocket'],
  'extraHeaders': {'foo': 'bar'} // optional
});

socket!.onConnect((data) {
  log('connected :'+ data);
});
socket!.onError((data) => log("Error: " + data));
socket!.on('event', (data) => log(data));
socket!.onDisconnect((_) => log('disconnect'));
socket!.on('message', (msg) => log("message from server: "+msg));
socket!.send(['test']);
log("listeners are set.");

}

What I get logged in my server is something like polling, every few seconds:

192.168.90.151 - - [11/Apr/2022 03:14:12] "GET /socket.io/?EIO=3&transport=websocket HTTP/1.1" 400 195 0.000999
(3088) accepted ('192.168.90.151', 50132)
192.168.90.151 - - [11/Apr/2022 03:14:17] "GET /socket.io/?EIO=3&transport=websocket HTTP/1.1" 400 195 0.000000
(3088) accepted ('192.168.90.151', 50136)
192.168.90.151 - - [11/Apr/2022 03:14:22] "GET /socket.io/?EIO=3&transport=websocket HTTP/1.1" 400 195 0.000000
(3088) accepted ('192.168.90.151', 50138)
192.168.90.151 - - [11/Apr/2022 03:14:27] "GET /socket.io/?EIO=3&transport=websocket HTTP/1.1" 400 195 0.000000
(3088) accepted ('192.168.90.151', 50140)
192.168.90.151 - - [11/Apr/2022 03:14:32] "GET /socket.io/?EIO=3&transport=websocket HTTP/1.1" 400 195 0.000000
....

But my listeners (neither in server nor in client) are not called.

1

There are 1 best solutions below

0
On

The issue was that my client and server socket.io library versions didn't match.

It was mentioned in the documentation but I didn't pay attention to it:

socket.io-client-dart   Socket.io Server
v0.9.* ~                v1.*    v2.*
v2.*                    v3.* & v4.*

I upgraded my client version to socket_io_client: ^2.0.0-beta.4-nullsafety.0 and it got fixed.