Flutter Socket.io not connecting to domain

Viewed 1173

I've been trying to connect to socket.io via flutter with no success. So far I tried:

1. flutter_socket_io:

SocketIO socket = SocketIOManager().createSocketIO(
    "https://my-socket.example.com", "",
    query: "", socketStatusCallback: _socketStatus);
socket.init();
socket.connect();
...
_socketStatus(dynamic data) { 
   print("Socket status: " + data); 
}

status callback shows:

D/FlutterSocketIoPlugin: SocketIO(20041): connect_error: [{"cause":{"detailMessage":"Expected HTTP 101 response but was \u0027400 Bad Request\u0027","stackTrace":[],"suppressedExceptions":[]},"detailMessage":"websocket error","stackTrace":[],"suppressedExceptions":[]}]

2. socket_io_client

socket = IO.io('https://my-socket.example.com', <String, dynamic>{
  'transports': ['websocket'],
  'autoConnect': false,
  'verify': false,
  'extraHeaders': {'foo': 'bar'},
  'query': {'user': guid}
});
socket.on('connect_error', (c) {
  log(c.toString());
});
socket.connect();

connect_error event log:

[log] WebSocketException: Connection to 'https://my-socket.example.com:0/socket.io/?user=d9c5d5bd-a831-43ed-9a59-20dbac14da8a&EIO=3&transport=websocket#' was not upgraded to websocket

I have been reading about this here: websocket not upgraded error #11444 but still not getting it

my nginx server is configured as:

 listen [::]:443 ssl; # managed by Certbot
 listen 443 ssl; # managed by Certbot
 ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
 ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
 include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
 ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

location /
    {
            proxy_pass http://127.0.0.1:3003;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-NginX-Proxy true;
            proxy_cache_bypass $http_upgrade;
    }

The socket is working correctly if I connect via VueScoketIO client as C# client, but not with flutter... tried changing the protocol to http, ws, wss but nothing.

Tried and works if I connect locally to 127.0.0.1, as works if I connect to local ip like 192.168... but not to the production domain.

Tried also to connect to ws://echo.websocket.org and does not work too.

0 Answers
Related