socket.io-client-dart icon indicating copy to clipboard operation
socket.io-client-dart copied to clipboard

I'm getting on connect_error timeout

Open heshaShawky opened this issue 5 years ago • 18 comments

I'm using NodeJS/NestJS for server-side socket.io

And your lib for client-side ( Flutter ) So I first have tried the example code and completely just nothing!!

So then I went to use on with connect_error to check if there are any errors or something and it showed just one word on the console every couple of seconds and it's timeout

heshaShawky avatar Feb 21 '20 03:02 heshaShawky

This example works for me - https://github.com/rikulo/socket.io-client-dart/issues/36#issuecomment-588580832

jumperchen avatar Feb 21 '20 04:02 jumperchen

I have the same problem with you

a1245582339 avatar Feb 23 '20 04:02 a1245582339

This issue still persists. Is there any solution?

Mavennix avatar Apr 29 '20 21:04 Mavennix

Can any example code be shown here?

jumperchen avatar May 05 '20 07:05 jumperchen

The reconnect logic does not work well. Try connect to socket.io and then disable your WiFi, let it error awhile, then enable Wifi back. The reconnect logic is in the failed loop even the internet connection is restored.

boreys avatar Jul 03 '20 20:07 boreys

I have the same problem

kamyabrs avatar Aug 24 '20 15:08 kamyabrs

This example works to me:

Flutter

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:socket_io_client/socket_io_client.dart' as IO;


void main(){
  runApp(
      new MaterialApp(
        title: 'Hello World App',
        home: new MyApp(),
      )
  );
}

class MyApp extends StatefulWidget {
  @override
  _State createState() => new _State();
}

class _State extends State<MyApp>{
  String _textFromServer = "";
  IO.Socket _socket;

  @override
  void initState() {
    super.initState();
    _socket = IO.io('http://10.1.3.95:3000/', <String, dynamic>{'transports': ['websocket'], 'forceNew': true});
    _socket.connect();
    _socket.on("connect", (_) {
          print('Connected');
          Timer.periodic(const Duration(seconds: 1), (Timer countDownTimer) {
            _socket.emit('toServer', json.encode({'key': 'hello', 'value': 'world'}));
        });
    });
    _socket.on("fromServer", (_) {
      print('fromServer $_');
    });
    _socket.on("connect_error", (data) => print('connect_error: $data'));
    _socket.on("reconnect", (data) => print('reconnect: $data'));
    _socket.on("reconnect_failed", (data) => print('reconnect_failed: $data'));
    _socket.on("reconnect_error", (data) => print('reconnect_error: v'));

  }
  void _updateText() {
    _socket.emit('toServer', json.encode({'key': 'hello', 'value': 'world'}));
    print('send text to server');
  }
  @override
  Widget build(BuildContext context) {
    print('build');
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text(_textFromServer),
        ),
        body: Center(
          child: Text('Hello World'),
        ),
       floatingActionButton: FloatingActionButton(
        onPressed: _updateText,
        tooltip: 'Update Text',
        child: Icon(Icons.update),
      ),
      ),
    );
  }
}

Dart Server

import 'package:socket_io/socket_io.dart';

void main() {
  // Dart server
  var io = Server();
  io.on('connection', (client) {
    final headers = client.handshake['headers'];
    headers.forEach((k, v) => print('$k => $v'));

    print('connection default namespace');
    client.on('toServer', (data) {
      print('data from default => $data');
      client.emit('fromServer', '$data');
    });
  });
  io.listen(3000);
}

Result

I/flutter (10590): build
I/flutter (10590): Connected
I/flutter (10590): fromServer {"key":"hello","value":"world"}
I/flutter (10590): fromServer {"key":"hello","value":"world"}
I/flutter (10590): connect_error: SocketException: OS Error: Network is unreachable, errno = 101, address = 10.1.3.95, port = 54218
I/flutter (10590): reconnect_error: v
I/flutter (10590): connect_error: SocketException: OS Error: Network is unreachable, errno = 101, address = 10.1.3.95, port = 54220
I/flutter (10590): reconnect_error: v
I/flutter (10590): connect_error: SocketException: OS Error: Network is unreachable, errno = 101, address = 10.1.3.95, port = 54222
I/flutter (10590): reconnect_error: v
I/flutter (10590): connect_error: SocketException: OS Error: Network is unreachable, errno = 101, address = 10.1.3.95, port = 54224
I/flutter (10590): reconnect_error: v
I/flutter (10590): connect_error: SocketException: OS Error: Network is unreachable, errno = 101, address = 10.1.3.95, port = 54226
I/flutter (10590): reconnect_error: v
I/flutter (10590): reconnect: 6
I/flutter (10590): Connected
I/flutter (10590): fromServer {"key":"hello","value":"world"}
I/chatty  (10590): uid=10135(com.example.flutter_app) Thread-2 identical 22 lines
I/flutter (10590): fromServer {"key":"hello","value":"world"}
I/flutter (10590): fromServer {"key":"hello","value":"world"}
I/flutter (10590): fromServer {"key":"hello","value":"world"}
I/flutter (10590): fromServer {"key":"hello","value":"world"}
I/flutter (10590): fromServer {"key":"hello","value":"world"}

jumperchen avatar Aug 26 '20 03:08 jumperchen

I had similar problem when I was starting to test this library for my project and I had 2 problems with the Node.JS server.

First was that I could not use Socket.IO version 3 with, it had same problem on web sockets. Second thing I did was that I used ngrok for my server and used that one to connect.

Jhellsten avatar Jan 11 '21 22:01 Jhellsten

@Jhellsten For Socket.io V3, please use v2.0.0-beta version - https://pub.dev/packages/socket_io_client/versions/2.0.0-beta.2

jumperchen avatar Jan 13 '21 03:01 jumperchen

I'm also using a NestJS server and it doesn't seem to connect even when it receives 101 from the server. Following up on all the issues and guidelines in the repository isn't helping and neither is the "it's working for me" comment.

This is what I have written on the client

        IO.io('https://game.stage.codeblitz.app/', <String, dynamic>{
      'transports': ['polling'],
      'port': '443',
      'forceTrue': true
    });

This is what I'm using on web

let g = io('/game', { transport:'websocket', query: { transport: 'websocket', 'X-Game-Id': 'abc' }})

I'm using namespaces, is this what creating the issue? Is there any other thing you'll require to debug this issue?

jatinkatyal13 avatar Feb 13 '21 12:02 jatinkatyal13

I'm using

  • nodejs socketio v^3.1.2 and ^4.4.0
  • socketio-client v2.0.0-beta.4-nullsafety.0

and now it works.

Socket socket = io(
      Config.SocketIOUrl,
      OptionBuilder().setTransports(['websocket'])
          .setExtraHeaders({'foo': 'bar'})
          .build());

  socket.onConnectError((error) {
    print('connect');
    socket.emit('chatMessage', 'test');
  });
  socket.onConnect((_) {
    print('connect');
    socket.emit('chatMessage', 'test');
  });
  socket.onDisconnect((_) {
    print('disconnect');
  });

binhphi109 avatar Feb 23 '22 21:02 binhphi109

I'm using

  • nodejs socketio v^3.1.2 and ^4.4.0
  • socketio-client v2.0.0-beta.4-nullsafety.0

and now it works.

Socket socket = io(
      Config.SocketIOUrl,
      OptionBuilder().setTransports(['websocket'])
          .setExtraHeaders({'foo': 'bar'})
          .build());

  socket.onConnectError((error) {
    print('connect');
    socket.emit('chatMessage', 'test');
  });
  socket.onConnect((_) {
    print('connect');
    socket.emit('chatMessage', 'test');
  });
  socket.onDisconnect((_) {
    print('disconnect');
  });

How ???

RikkeiDN-TuPT avatar Aug 18 '22 09:08 RikkeiDN-TuPT

I have the same error! I copied your code into another new project and run that but still it can't connect to the server. My backend is nodjs. I have a next js web app too and it has no problem with backend. It sends and receive data with no problem. but in flutter side it's giving timeout error.

Hamid313-coder avatar Feb 02 '23 10:02 Hamid313-coder

I have the same error. It was working a few days ago. Even though I changed nothing, it is not working now, it gives a timeout error. Were you able to find a solution ?

eliffkaragoz avatar Feb 10 '23 11:02 eliffkaragoz

I have the same error. It was working a few days ago. Even though I changed nothing, it is not working now, it gives a timeout error. Were you able to find a solution ?

I am having a similar issue, can anyone provide a solution

Karthikeya-01 avatar Feb 16 '23 12:02 Karthikeya-01

Same problem here Connect Error: timeout. Anyone can provide us with a solution?

Muswamba avatar Dec 16 '23 14:12 Muswamba

same error

SabariFuzionest avatar Feb 01 '24 18:02 SabariFuzionest