Skip to content
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

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

Closed
1 of 2 tasks
noahprince22 opened this issue Oct 30, 2018 · 48 comments
Closed
1 of 2 tasks

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

noahprince22 opened this issue Oct 30, 2018 · 48 comments

Comments

@noahprince22
Copy link

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
@noahprince22
Copy link
Author

Looks like this is react-native's issue. Earlier versions of RN seem to work with socket.io, but 0.0.57 does not. Closing.

@rxhivzero
Copy link

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

@khloukrada
Copy link

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

Yes it does work.

@jr-k
Copy link

jr-k commented Jan 3, 2019

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

@linux08
Copy link

linux08 commented Jan 23, 2019

@noahprince22 can you please share your configuration ?
I am currently using this and the issue still persist.

"react-native": "0.55.4",
"react": "16.3.1",
"socket.io-client": "^2.0.4",
"socket.io: "^2.2.0" //server side

@ningacoding
Copy link

any workaround?

socket.io team please, this should work with RN, is a socket.io bug.

@linux08
Copy link

linux08 commented Mar 1, 2019

Hi @ningacoding , I managed to get it working with this configuration

"react-native": "0.55.4",
"react": "16.3.1",
"socket.io-client": "^2.0.4",
"socket.io: "^1.0.0" //server side

In your react-native app use this

const socket = io(BASE_URL, {
    secure: true,
    transports: ['websocket'],
  });

@SukumarSmart
Copy link

"react-native": "0.58.6",
"react": "16.6.3",
"socket.io-client": "^2.0.4",

Not working for me

@LucasHimelfarb
Copy link

any solution? I'm facing the same thing

@noahprince22
Copy link
Author

I have long since stopped working on the project that I was having this issue with. If I remember correctly, I downgraded react-native to "solve' the problem.

@russellgoldman
Copy link

Experiencing the same issue, has there been a fix yet? Anyone find any workarounds to implementing sockets in React Native?

@anttirauh
Copy link

anttirauh commented Sep 23, 2019

@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.

@Ramesh21071993
Copy link

Ramesh21071993 commented Sep 24, 2019

Hi @anttirauh and @russellgoldman,
I tried with RN 0.59.10 and Socket 2.0.4, but its not connecting on release build.
Can you please help me?

@anttirauh
Copy link

@Ramesh21071993 I think you must upgrade socket.io to version 2.1.1.

@Ramesh21071993
Copy link

@anttirauh, i tried with all these versions socket io (2.0.4, 2.1.1, 2.2.0) and react-native versions (0.60, 0.59.10, 0.59.4)
In server side also socket io 2.0.4.
still that same issue sir,

But it works on RN 0.55.4

@anttirauh
Copy link

@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.

@Ramesh21071993
Copy link

Ramesh21071993 commented Sep 25, 2019

@anttirauh Sir, below is my package.json. I did as what you told. But nothing happen. I remove build folder and node modules and again i install.
Please help me on this to work, Past one month am trying

App package.json

{
"name": "Track",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.8.3",
"react-native": "0.59.10",
"socket.io": "2.1.1"
},
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/runtime": "^7.6.2",
"babel-jest": "^24.9.0",
"jest": "^24.9.0",
"metro-react-native-babel-preset": "^0.56.0",
"react-test-renderer": "16.8.3"
},
"jest": {
"preset": "react-native"
}
}

Server Package.json

{
"name": "TRACK-Socket",
"version": "0.0.1",
"dependencies": {
"amqplib": "^0.5.1",
"config": "^3.2.2",
"express": "^4.15.3",
"socket.io": "^2.1.1",
"ws": "^7.1.2"
}
}

Thanks

@rastapasta
Copy link

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 😆

@Ramesh21071993
Copy link

@rastapasta , i got an error like “CLEARTEXT communication not permitted by network security policy”,

Please tell me what to do

@rastapasta
Copy link

@Ramesh21071993 if you can't use your endpoint via https (please consider it), add their hostnames to the exception lists.

for iOS: https://stackoverflow.com/a/30732693
for Android: https://stackoverflow.com/a/50834600

@Ramesh21071993
Copy link

Ramesh21071993 commented Sep 26, 2019

Sir, I tried that also but not able to do. If you don't mind can you please give me any demo source files
For long time I struck with this.

Am using ip and port sir

@rastapasta
Copy link

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)

@Ramesh21071993
Copy link

Sir, in server side am using socket io 2.1.1, is it ok or anything need to change

@Lizhooh
Copy link

Lizhooh commented Oct 11, 2019

image

@rastapasta
Copy link

@Lizhooh hacky way of finding out whazzup on rn: monkey patch a console.log(err) before the self.onError in your node_modules engine.io-client/blob/master/lib/transports/polling-xhr.js #L131

@deepakburnwal5
Copy link

@Ramesh21071993 Is your issue resolved? I am facing the same issue. Tried everything mentioned in above comments.

@deepakburnwal5
Copy link

deepakburnwal5 commented Nov 9, 2019

@rastapasta I followed what you have suggested above. My socket connection happens and then immediately disconnects. I get timeout error infinitely. However, whenever I click on some component which makes a network call, the socket connects and immediately disconnects. If I don't do any network call after first socket disconnect, it would stay in the same state. Please, could you help?

socket = io('http://IP:31515', {
path: '/socketio', //I have given this is the path in my socket server as well as my kubernetes ingress file to route to that port
transports: ['websocket'],
reconnectionDelay: 1000,
reconnection: true,
reconnectionAttempts: Infinity,
// timeout: 30000,
jsonp: false
});

things that I have tried:

  1. removing and adding transports option
    2.removing and add timeout
  2. removing all parameters except path
  3. adding pingTimeout and pingInterval in server socket configuration
  4. removing path from server and client and only keeping it in my kubernetes ingress file to route to that deployment/pod.

Note: the same server and kubernetes conf work for any node.js socket client as well as my react js web app. I am only facing this issue for React Native app in debug as well as release mode.

@deepakburnwal5
Copy link

Does anyone have any idea for my above issue?

@rastapasta
Copy link

@calqlum123
Please make sure to

  • add your host to the transport exception list (s. here)
  • try it without manually defining the transports
  • monkey patch the console.log (s. here) and check the error

@HZSamir
Copy link

HZSamir commented Dec 8, 2019

Can anyone please share a complete working example for both client and server? Because this is confusing.

@rastapasta
Copy link

@Unforgiven-wanda

foodsharing.network's socket io:

@ashishonmobile
Copy link

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.

@paliwalsam
Copy link

you must add this code in application tag
like below in AndroidManifest.xml
<Application . . android:usesCleartextTraffic="true"> . . .

@avinashsahoo
Copy link

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"

@darrachequesne
Copy link
Member

@Unforgiven-wanda an example with React Native has been added here: https://github.com/socketio/socket.io/tree/master/examples/react-native

@chiragpurohit71085
Copy link

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

@darrachequesne
Copy link
Member

@chiragpurohit71085 have you checked the example there: https://github.com/socketio/socket.io/tree/master/examples/react-native?

I think that this is a problem of URL, could you please check if your server is reachable at http://192.168.x.y:z?

@HamedNN76
Copy link

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",

@darrachequesne
Copy link
Member

@HamedNN76 if setting transports: ['websocket'] (that is, disabling HTTP long-polling) is required to fix your issue, it might be related to CORS:

const { Server } = require("socket.io");

const io = new Server({
  cors: {
    origin: ["http://localhost:19006"]
  }
});

Documentation: https://socket.io/docs/v4/handling-cors/

I don't think disabling JSONP (jsonp: false) is required here, in any case.

It could also be related to the version of your client (2.1.1). Please check the compatibility table here: https://socket.io/docs/v4/client-installation/#Version-compatibility

@arshadazaad3
Copy link

arshadazaad3 commented Oct 7, 2021

@Unforgiven-wanda an example with React Native has been added here: https://github.com/socketio/socket.io/tree/master/examples/react-native

Works!

"react-native": "0.63.4",
"socket.io": "^4.0.0",
"socket.io-client": "^4.0.0",

Edit: Comment Outdated! - 24/04/2024

@techiekarthick
Copy link

Can anyone please suggest, not able to connect socket-io-client

"react": "18.1.0",
"react-native": "^0.70.6",
"socket.io-client": "^4.5.0",

It throwing 'connect_error' ? Any suggestion to resolve

@darrachequesne
Copy link
Member

@techiekarthick hi! did you check our troubleshooting guide?

@techiekarthick
Copy link

@darrachequesne hi, yeah I'm already checked.

@darrachequesne
Copy link
Member

@techiekarthick could you please share the details of the error object:

socket.on("connect_error", (error) => {
  console.log(error);
});

@techiekarthick
Copy link

techiekarthick commented Jan 25, 2023

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

@techiekarthick
Copy link

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

@ilkanozen
Copy link

npm install socket.io-client not installing supported version for socket.io(server-side)
when i tryed
npm install socket.io@latest (server-side)
npm install socket.io-client@latest(client-side)
it's worked for me

@darrachequesne
Copy link
Member

For future readers:

Please check our React Native guide here: https://socket.io/how-to/use-with-react-native

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

No branches or pull requests