socket.io-client icon indicating copy to clipboard operation
socket.io-client copied to clipboard

Unable to connect on React-Native (only in Debug Mode)

Open jr-k opened this issue 5 years ago • 20 comments

You want to:

  • [x] 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:

There is nothing only a timeout triggered

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: Android 8.0
  • browser: React-native
  • socket.io version: 2.1.1 (but tried differents verisons with same result)

jr-k avatar Jan 03 '19 21:01 jr-k

oops duplicate of https://github.com/socketio/socket.io-client/issues/1245

jr-k avatar Jan 03 '19 21:01 jr-k

I'm reopening this issue, this is slightly different.

I tried to install socket io on a fresh react-native installation 0.57.8 and I'm facing this issue again.

jr-k avatar Jan 03 '19 22:01 jr-k

Facing same issue. Not working in RN version 0.57.3. Works fine when connects to local server. But it is not connecting to the main backend server.

anooj1483 avatar Jan 09 '19 04:01 anooj1483

React Native: 0.57.8 React: 16.6.3 socket.io-client: 2.0.4

This worked for me. Also need to change the socketio js version in server side to 2.0.4

anooj1483 avatar Jan 11 '19 16:01 anooj1483

I'm facing the same issue

@jr-k 's issue persists after @anooj1483 's hack according to https://github.com/socketio/socket.io-client/issues/1245#issuecomment-451285245 in #1245 It is the same for me

EliasTouil avatar Jan 14 '19 09:01 EliasTouil

Facing the same issue. Works well when debug mode is on, but doesn't work when Debug mode is turned off or apk is built with release mode.

Versions:
"react": "16.8.2",
"react-native": "0.58.4",
"socket.io-client": "^2.2.0"

mostafiz93 avatar Feb 18 '19 11:02 mostafiz93

Same issue as everyone else. Works perfectly if debugging. React Native App versions: "react": "16.5.0", "react-native": "^0.58.4", "socket.io-client": "^2.1.1"

Server Version: "socket.io": "2.0.4"

Reardonp avatar Feb 18 '19 17:02 Reardonp

socket.io-client: 2.0.4 worked for me.

mostafiz93 avatar Feb 19 '19 05:02 mostafiz93

I'm getting error on ussing 2.0.4 + ReactNative

TypeError: undefined is not an object (evaluating 'n.protocol')

image

ningacoding avatar Feb 28 '19 22:02 ningacoding

FIXED IT for me when i was adding host:port like next in debug mode:

const socket = io("//domain.com:port", opts); // this works only in debug mode

didn't worked in NO debug mode, i had to add the protocol:

const socket = io("http://domain.com:port", opts);

ningacoding avatar Mar 01 '19 03:03 ningacoding

I have the same issue react-native : 0.57.8, socket-io-client : 2.2.0 only works when debug is enabled

reza-madani avatar May 09 '19 19:05 reza-madani

has anyone found a fix for this yet? i have tried several different versions of the package with no luck

jaredstevick avatar May 12 '19 00:05 jaredstevick

there are many causes of this error/bug, mine was only one of many.

ningacoding avatar May 12 '19 02:05 ningacoding

try last version from github, like this, solved my issues with RN android:

npm i socketio/engine.io-client#3.3.2 -S

Cheers

diegolaciar avatar Jul 15 '19 21:07 diegolaciar

FIXED IT for me when i was adding host:port like next in debug mode:

const socket = io("//domain.com:port", opts); // this works only in debug mode

didn't worked in NO debug mode, i had to add the protocol:

const socket = io("http://domain.com:port", opts);

its works on socket.io 2.1.1 and react-native 0.59.8. love you @ningacoding 👍 👍

autotrof avatar Sep 03 '19 02:09 autotrof

Hi @autotrof, I tried with react-native 0.59.8 and socket.io, but it can't connect in release build. Am using ip and port to connect, Can you please help me?

Ramesh21071993 avatar Sep 24 '19 20:09 Ramesh21071993

Anyone cannot solved yet? In my case

react-native : 0.61.5
socket.io-client: 2.3.0 (I tried another version, 2.1.1, 2.0.1, 2.1.0)

I likewise only in debug mode worked :(

karriz-dev avatar Mar 10 '20 02:03 karriz-dev

I solved this problem change console.dir() to console.log() console.dir() function is not support to release mode 😃😃

karriz-dev avatar Mar 10 '20 08:03 karriz-dev

try this on android it worked for me

<application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="false" android:usesCleartextTraffic="true" <-- add this

berto6544 avatar Nov 24 '20 10:11 berto6544

"react-native": "0.70.5", "socket.io-client": "^4.6.1",

This how i setup my socket connection as per the docs

import { io } from "socket.io-client"

const URL = "http://localhost:3030"
const socket = io(URL, { autoConnect: false })
socket.onAny((event, ...args) => {
  console.log("All event listener...", event, args)
})
export default socket

Only working in debug mode

dtembo98 avatar Apr 27 '23 07:04 dtembo98

For future readers:

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

Please reopen if needed.

darrachequesne avatar Apr 10 '24 10:04 darrachequesne