socket.io-client-dart
socket.io-client-dart copied to clipboard
I'm getting on connect_error timeout
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
This example works for me - https://github.com/rikulo/socket.io-client-dart/issues/36#issuecomment-588580832
I have the same problem with you
This issue still persists. Is there any solution?
Can any example code be shown here?
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.
I have the same problem
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"}
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 For Socket.io V3, please use v2.0.0-beta version - https://pub.dev/packages/socket_io_client/versions/2.0.0-beta.2
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?
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');
});
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 ???
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.
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 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
Same problem here Connect Error: timeout. Anyone can provide us with a solution?
same error