socket.io-client: Can't connect via socket.io-client on react-native

You want to:

  • report a bug
  • request a feature

Current behaviour

I can’t connect via socket io to my server, it times out.

Steps to reproduce (if the current behaviour is a bug)

In react-native:

import io from 'socket.io-client';


const BACKEND = 'my socketio server url'

const sock = io(BACKEND, { transports: ['websocket'], forceNew: true });
sock.on('connected', () => {
  debugger;
})

The debug logs look like:

socket.io-client:url parse <my url> +0ms
browser.js:133 socket.io-client ignoring socket cache for <my url> +0ms
browser.js:133 socket.io-client:manager readyState closed +0ms
browser.js:133 socket.io-client:manager opening <my url> +1ms
browser.js:133 engine.io-client:socket creating transport "websocket" +0ms
browser.js:133 engine.io-client:socket setting transport websocket +3ms
browser.js:133 socket.io-client:manager connect attempt will timeout after 20000 +6ms
browser.js:133 socket.io-client:manager readyState opening +2ms
socket.io-client:manager connect attempt timed out after 20000 +20s
browser.js:133 engine.io-client:socket socket close with reason: "forced close" +20s
browser.js:133 engine.io-client:socket socket closing - telling transport to close +2ms
browser.js:133 socket.io-client:manager connect_error +3ms
browser.js:133 socket.io-client:manager cleanup +1ms
browser.js:133 socket.io-client:manager will wait 899ms before reconnect attempt +1ms
browser.js:133 socket.io-client:manager attempting reconnect +912ms

Note: the best way (and by that we mean the only way) to get a quick answer is to provide a failing test case by forking the following fiddle.

Expected behaviour

I expect to be able to connect to my socketio server, the same way I am able to in a web browser.

Setup

  • OS: iOS iPhone 10 simulator
  • browser: React-native
  • socket.io version: 2.1.1

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 48 (6 by maintainers)

Most upvoted comments

Finally found a solution for React Native >= 0.60 after a couple of coffees and digging deep into engine.io - additional to a version conflict (with RN) there seems to be an issue with passing in the URI. When I pass an URI into socketIO, it gets turncated to the host and ‘socket.io’ appened. So socketIO("https://foo.bar/my/endpoint/socket.io") will lead to socketIO trying to connect to https://foo.bar/socket.io without showing it in the logs when throwing the xhr pull / socket error.

Here we go, how to get socket.io running on current versions of React Native:

  • use socket.io-client version 2.1.1
// package.json
{
  "dependencies": {
     "socket.io-client": "2.1.1"
   }
}
  • when setting up the client, define a host and a path
const socket = socketIO('https://your.host.com/', {
  path: '/path/to/socket.io/'
})
  • and suddenly everything works as smooth as planed

Hope to have saved you the time I spent on this issue 😆

Try “socket.io-client”: “2.0.4” This may be socket.io bug

@russellgoldman I had problems with RN 0.60.5 and socket.io. Release build did not connect to the server.

None of socket.io versions worked with RN 0.60.5: I tried versions 2.0.4, 2.1.1, 2.2.0.

Downgrading to RN 0.59.10 solved my problem.

I am using “react-native”: “0.61.5” and “socket.io-client”: “^2.1.1”,

below is my code

const socket = SocketIOClient('http://192.168.0.101:1234',{reconnect: true}); socket.connect(); socket.once("connect", () => { console.log("[CLIENT] Connected to remote server, identificating."); // this.sendIdentification(); }); socket.on('connect', () => { console.log('connected to socket server'); });

The code is not able to connect to the server and I am also looking for the solution. I don’t want to downgrade my react-native version.

Can you try “ws://192.168.0.101:1234”, instead of “http://192.168.0.101:1234

@Ramesh21071993 My understanding (which is formed with heavy googling and own testing) is that:

  • socket.io stopped working with react-native about around RN version 0.57.4
  • socket.io or some other component was fixed later and I managed to get it working with version combo: RN 0.59.10 + socket.io 2.1.1
  • RN version 0.60.x is not working with any version of socket.io

So my advice is only: try one time more with RN 0.59.10 and socket.io 2.1.1 and make sure that you clear build folder before building. And remove node_modules before yarn/npm.

I am using “react-native”: “0.61.5” and “socket.io-client”: “^2.1.1”,

below is my code

const socket = SocketIOClient('http://192.168.0.101:1234',{reconnect: true}); socket.connect(); socket.once("connect", () => { console.log("[CLIENT] Connected to remote server, identificating."); // this.sendIdentification(); }); socket.on('connect', () => { console.log('connected to socket server'); });

The code is not able to connect to the server and I am also looking for the solution. I don’t want to downgrade my react-native version.

With RN 0.52 and socket.io-client 2.0.4 I’m still facing this issue.

@darrachequesne Hi, I found the issue; there is no version problem. (WIFI) IP address mismatch between my laptop and my phone.

@darrachequesne It showing only this [Error: websocket error]

Finally, after so much research, I find out that you need to pass two parameters to io(), as shown below:

import io, { Socket } from 'socket.io-client';
...
const socket: Socket = io('http://localhost:8080', {
    transports: ['websocket'],
    jsonp: false,
});

package.json file:

"react": "17.0.1",
"react-native": "0.64.2",
"socket.io-client": "2.1.1",

Not able to connect with “socket.io-client”: “^3.0.4” in react native

follow the directions of above documentations and adapt this implementation:


import socketIO from 'socket.io-client'
const socket = socketIO('http://8.8.8.8:3000/', {
  path: '/your/path/to/socket.io/'
  reconnectionDelay: 1000,
  reconnection: true,
  reconnectionAttempts: Infinity,
  jsonp: false
})

socket.on('connect', () => console.log('connected'))
socket.on('error', console.error)
socket.on('connect_error', console.error)

any solution? I’m facing the same thing