Flutter Socket IO with nodejs - receiving timeout on connection

2,126

The issue being faced was due to the mis-match between the server version and the package being installed with Flutter. The version on the server was > 3. The latest version installed for flutter is not compatible with a server version. There is a beta version that needs to be used.

At the moment there are two options :

Option 1: Downgrade the servers version so that its compatible.

Option 2 use the beta version. (this is the option I took).

Update pubspec.yaml to socket_io_client: ^2.0.0-beta.2 and it connects.

Share:
2,126
user1012500
Author by

user1012500

Updated on December 28, 2022

Comments

  • user1012500
    user1012500 10 months

    I'm using flutter socket io to communicate to my server that's running node/express.

    The server code:

    var express = require('express');
    var bodyParser = require('body-parser')
    var app = express();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    var mongoose = require('mongoose');
    
    app.use(express.static(__dirname));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: false}))
    
    var Message = mongoose.model('Message',{
      name : String,
      message : String
    })
    
    app.get('/', (req, res) =>{
        res.send("Hello");
    
    });
    
    io.on('connection', () =>{
      console.log('a user is connected')
    });
    
    var server = http.listen(8080, "<MyServerIP>", () => {
      console.log('server is running on port', server.address().port);
    });
    

    My Flutter code :

    connect() async {
        try {
          
          String connectionPoint = "http://<MyServerIP>:8080";
    
          //Connect to Socket.IO
          socket = IO.io(
              connectionPoint,
              OptionBuilder()
                  .setTransports(['websocket']) // for Flutter or Dart VM
                  //.disableAutoConnect() // disable auto-connection
                  //.setExtraHeaders({'id': tokenId}) // optional
                  .build());
    
          //socket.connect();
          socket.onConnecting((data){
            print("Connecting");
          });
    
          
    
          socket.onConnectError((data) {
            
            print("Error Connecting - > $data");
            
            });
    
          socket.onConnectTimeout((data) => null);
    
          socket.onDisconnect((data) => null);
    
         
        } catch (e) {}
      }
    

    When ever i try to connect I'm getting a timeout error that's caught in onConnectError.

    The node server is running debian, and I've checked the firewall status:

    To                         Action      From
    --                         ------      ----
    27017                      ALLOW       Anywhere                  
    80                         ALLOW       Anywhere                  
    3000                       ALLOW       Anywhere                  
    22                         ALLOW       Anywhere                  
    Samba                      ALLOW       Anywhere                  
    8080                       ALLOW       Anywhere                  
    27017 (v6)                 ALLOW       Anywhere (v6)             
    80 (v6)                    ALLOW       Anywhere (v6)             
    3000 (v6)                  ALLOW       Anywhere (v6)             
    22 (v6)                    ALLOW       Anywhere (v6)             
    Samba (v6)                 ALLOW       Anywhere (v6)             
    8080 (v6)                  ALLOW       Anywhere (v6)             
    
    3000                       ALLOW OUT   Anywhere                  
    3000 (v6)                  ALLOW OUT   Anywhere (v6)   
    

    When I open the url via chrome I'm getting the "Hello" message. When I try netcat "nc -vz MyServerIp 8080 and I'm getting a success in connecting. I've also checked my local firewall and I've allowed all connections for qemu to my MyServerIp.

    Just need some help as to try to work out why I'm getting the timeout and whether there are any ways to debug this ?

    ##Edit: MyServerIP is the actual server ip of my server.

    ##Edit 2: I used my device to test whether it was something that was local issue to the android emulator. And, I received the same error (I also took the device off my wifi to eliminate any local firewall issues). I'm assuming that this would mean that it has something to do with my server.

    • Hadi Norouzi
      Hadi Norouzi over 2 years
      maybe it because your url is localhost:8080. replace it with ip address
    • user1012500
      user1012500 over 2 years
      Hi <MyServerIP> is actually the ip of my actual server hosted on a digitalocean. I've just replaced the the address on stackoverflow so that its not public.