Skip to content

Not able to connect to nodeJS server on localHost #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LucasMorais10 opened this issue Feb 20, 2020 · 14 comments
Closed

Not able to connect to nodeJS server on localHost #62

LucasMorais10 opened this issue Feb 20, 2020 · 14 comments

Comments

@LucasMorais10
Copy link

Hi, i'm trying to stabilish a connection between a nodeJS server and a flutter app but I always receive connect_error on client side. If I try to connect by the browser, it's successful, so i think the problem is on client side.

My nodeJS server:

const app = require('express')()
const http = require('http').createServer(app)
var io = require('socket.io')(http)

io.on('connection', (client) => {  
    console.log('a user connected')

    client.on('message', (data) => {
        console.log('Message received --> ', data)

        // io.emit('chat', data)
    })
})

http.listen(5000, () => {  
    console.log('Listening on port 5000')
})

Client side is implemented on InitState as follows:

@override
  void initState() {
    //on xxx.xxx.xxx.xxx i'm inserting my local ip address
    socket = IO.io('http//xxx.xxx.xxx.xxx:5000/', <String, dynamic>{
      'transports': ['websocket']
    });

    socket.connect();
    socket.on("connect", (_) => print('Connected'));
    socket.on("connect_error", (data) => print('connect_error: ' + data));
    
    super.initState();
  }

The client keeps showing the output "connect_error: timeout".

Anybody can help? Thanks in advance.

@jumperchen
Copy link
Member

Which version are you using? (both client and server)

@jumperchen
Copy link
Member

@LucasMorais10 I try with your example in my env. with Android simulator and it works well.
Here is my env. version:

  • NodeJs Socket.io server: 2.3.0
  • Flutter 1.12.13+hotfix.7
  • Dart 2.7.0
  • socket_io_client 0.9.8

@LucasMorais10
Copy link
Author

Hi @jumperchen, thanks for the answer. Here are the versions for my env:

  • NodeJs Socket.io server: 2.3.0
  • Flutter 1.12.13+hotfix.8
  • Dart 2.7.0
  • socket_io_client 0.9.4

I updated socket_io_client to 0.9.8 and still getting the same error. Do you think it could be something wrong on my network?

@jumperchen
Copy link
Member

@LucasMorais10 Did you use it in your Android Studio? or in a real mobile device?
If it's in Android Studio, the simulator is a VM which should be able to access your server if possible.

@LucasMorais10
Copy link
Author

It is a real android device.

Both pc and android device are connected to the same router.

I also tried to disable all firewall on server side, but didn't help anyway.

I can ping between them and access the server on the Android web browser. I don't see why i can't connect with the flutter client :/

@theguywithideas
Copy link

Any Update on this? Getting the same problem

@jumperchen
Copy link
Member

@theguywithideas can you provide an example to demonstrate the issue you met?

@aakashpothepalli
Copy link

IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
    'transports': ['websocket'],
    'extraHeaders': {'foo': 'bar'} // optional
  });

make sure you add this

@theguywithideas
Copy link

@jumperchen @aakash9518 I was able to find the problem in my code and correct it. Thanks for the prompt replies though.

@aquibbaig
Copy link

@theguywithideas What was the issue cause I'm having the same problem?

@theguywithideas
Copy link

@aquibbaig the problem that I faced was because of the way this package is accepting the endpoint of socket.io. In my case it was https://subdomain.domain.com/socketio initially and the connection to that endpoint was getting rejected. I later found out that the library expects the url in the format https://subdomain.domain.com only. The /socketio part after that is being considered as a namespace and that's why my connection was getting established. I just changed my socketio server domain to https://someothersubdomain.domain.com and got rid of /socketio part and it got connected to the default namespace. Not sure if you are facing the same problem or not. I think they should mention this in docs.

@aquibbaig
Copy link

Thanks for your quick update. I was actually facing a CORS issue!

@lehno
Copy link

lehno commented Apr 30, 2020

Im having this problem, but my localserver is https. The error is:
CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate.

Any tips on how to use it with self signed cert?

@jumperchen
Copy link
Member

FYI: dart-lang/sdk#34284
Not sure whether it's related or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants