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)
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 tohttps://foo.bar/socket.iowithout 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:
socket.io-clientversion 2.1.1Hope to have saved you the time I spent on this issue 😆
Try “socket.io-client”: “2.0.4” This may be socket.io bug
@Unforgiven-wanda an example with React Native has been added here: https://github.com/socketio/socket.io/tree/master/examples/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.
Can you try “ws://192.168.0.101:1234”, instead of “http://192.168.0.101:1234”
@Unforgiven-wanda
foodsharing.network’s socket io:
@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.
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:
package.json file:
Not able to connect with “socket.io-client”: “^3.0.4” in react native
follow the directions of above documentations and adapt this implementation:
any solution? I’m facing the same thing