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
Comments
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. |
Try "socket.io-client": "2.0.4" |
Yes it does work. |
With RN 0.52 and socket.io-client 2.0.4 I'm still facing this issue. |
@noahprince22 can you please share your configuration ?
|
any workaround? socket.io team please, this should work with RN, is a socket.io bug. |
Hi @ningacoding , I managed to get it working with this configuration
In your react-native app use this
|
"react-native": "0.58.6", Not working for me |
any solution? I'm facing the same thing |
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. |
Experiencing the same issue, has there been a fix yet? Anyone find any workarounds to implementing sockets in React Native? |
@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. |
Hi @anttirauh and @russellgoldman, |
@Ramesh21071993 I think you must upgrade socket.io to version 2.1.1. |
@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) But it works on RN 0.55.4 |
@Ramesh21071993 My understanding (which is formed with heavy googling and own testing) is that:
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. |
@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. App package.json{ Server Package.json{ Thanks |
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 Here we go, how to get socket.io running on current versions of React Native:
// package.json
{
"dependencies": {
"socket.io-client": "2.1.1"
}
}
const socket = socketIO('https://your.host.com/', {
path: '/path/to/socket.io/'
})
Hope to have saved you the time I spent on this issue 😆 |
@rastapasta , i got an error like “CLEARTEXT communication not permitted by network security policy”, Please tell me what to do |
@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 |
Sir, I tried that also but not able to do. If you don't mind can you please give me any demo source files Am using ip and port sir |
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) |
Sir, in server side am using socket io 2.1.1, is it ok or anything need to change |
@Lizhooh hacky way of finding out whazzup on rn: monkey patch a |
@Ramesh21071993 Is your issue resolved? I am facing the same issue. Tried everything mentioned in above comments. |
@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', { things that I have tried:
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. |
Does anyone have any idea for my above issue? |
Can anyone please share a complete working example for both client and server? Because this is confusing. |
@Unforgiven-wanda foodsharing.network's socket io: |
I am using "react-native": "0.61.5" and "socket.io-client": "^2.1.1", below is my code
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. |
you must add this code in application tag |
Can you try "ws://192.168.0.101:1234", instead of "http://192.168.0.101:1234" |
@Unforgiven-wanda an example with React Native has been added here: https://github.com/socketio/socket.io/tree/master/examples/react-native |
Not able to connect with "socket.io-client": "^3.0.4" in react native |
@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 |
Finally, after so much research, I find out that you need to pass two parameters to io(), as shown below:
package.json file:
|
@HamedNN76 if setting 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 ( It could also be related to the version of your client ( |
Works! "react-native": "0.63.4", Edit: Comment Outdated! - 24/04/2024 |
Can anyone please suggest, not able to connect socket-io-client "react": "18.1.0", It throwing 'connect_error' ? Any suggestion to resolve |
@techiekarthick hi! did you check our troubleshooting guide? |
@darrachequesne hi, yeah I'm already checked. |
@techiekarthick could you please share the details of the error object: socket.on("connect_error", (error) => {
console.log(error);
}); |
@darrachequesne It showing only this |
@darrachequesne Hi, I found the issue; there is no version problem. (WIFI) IP address mismatch between my laptop and my phone. |
npm install socket.io-client not installing supported version for socket.io(server-side) |
For future readers: Please check our React Native guide here: https://socket.io/how-to/use-with-react-native |
You want to:
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:
The debug logs look like:
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
The text was updated successfully, but these errors were encountered: