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

Unable to connect and reconnect on react native

Open sandropoluan opened this issue 6 years ago • 13 comments

I was using this lib for a year and everything is going fine. I was start using RN 0.45, and 0.48 , and then 0.53.0. Everything is perfect.

Until I upgrade to react native 0.53.3, and this issue appear. this is the error I got {"type":"TransportError","description":{"line":"76","collumn":"7944","sourceURL":"index.android.bundle"}} sometimes i got "timeout"

I didn't change any code, I didn't change any dependency. What I did just:

  1. Deleting my node_modules folder
  2. In package.json I change RN and react version number.
  3. I run 'NPM install'

I thought it might the React native issue, so I roll back to the previews version I was used 0.53.0. But this problem still exist. I roll back again to 0.48, the issue still there. and so on.

I use socket.io-client 2.0.3 (I have try 2.0.4 but not help)

  • [x] report a bug
  • [ ] request a feature

Setup

  • OS: Windows 10 64bit
  • browser: React Native
  • socket.io version: 2.0.3

I have no Idea what is going on.
Hope someone will help. Thank you

sandropoluan avatar Mar 04 '18 07:03 sandropoluan

+1

Mifayo avatar Mar 07 '18 15:03 Mifayo

I resolved this by adding 'transports': ['websocket'], option

  this.socket = openSocket(socketUrl,{
          'reconnection': true,
          'reconnectionDelay': 500,
	  'reconnectionAttempts': Infinity, 
	  'transports': ['websocket'],            
      });

But that option is not required, am I right? So I keep this issue open because it will break when I omitting that option.

sandropoluan avatar Mar 08 '18 06:03 sandropoluan

i did have this issue after i add express to the server and added some routes and when i added 'transports': ['websocket'],
it works fine but it disconnect after a while and PS: it disconnect while i'm working with the app not in backround or something else.

Mifayo avatar Mar 08 '18 09:03 Mifayo

Could you please provide a way to reproduce? (I tried with jest with no luck: https://github.com/darrachequesne/jest/commits/test/socket.io)

darrachequesne avatar Mar 09 '18 11:03 darrachequesne

Setting transports to websocket also solved my problem, but of course turns off http polling which is unfortunate. Steps to reproduce:

  1. Install [email protected] in a react native project, add code to create a new socket io connection, and run the android emulator
  2. Ensure a node server is running [email protected] and enable debug logging Expected: the connection to establish (ios simulator successfully connects) Actual: socket io emits a connect_error on the client with a code of 'parse error'. Upon inspection, the xhr.response given is an empty string which socket io interprets as a parse error. The message that fails to parse seems to be the 'pong' message from the socket io server.

charlieblevins avatar May 11 '18 16:05 charlieblevins

I have the same problem on iOS, the temporary solution of adding the option 'transports': ['websocket'] also worked for me.

Configuration:

  • socket.io 2.1.1
  • react 16.3.2
  • react-native 0.52.0
  • iOS 11.2

cglacet avatar May 31 '18 14:05 cglacet

I am trying to connect socket.io on my react native android. however, it is not connecting. I tried some tutorials and iOS is working fine but Android is not. I have tried the 'transports':['websocket'] but still not working :( .. Any suggestions or ideas or sample codes?

My backend is Nodejs and it also used socket.io. Thanks!

Aung-Myint-Thein avatar Aug 03 '18 06:08 Aung-Myint-Thein

I am trying to connect socket.io on my react native android. however, it is not connecting. I tried some tutorials and iOS is working fine but Android is not. I have tried the 'transports':['websocket'] but still not working :( .. Any suggestions or ideas or sample codes?

My backend is Nodejs and it also used socket.io. Thanks! Did you find any solution to this i have the same problem on react-native on Android.

dalt45 avatar Sep 11 '18 15:09 dalt45

same issue

manpreetmobilyte avatar Sep 26 '18 23:09 manpreetmobilyte

hello guys I have a problem on socket

it show timeout when I check on

socket.on('connect_error', (err) => { console.log('error123',err)

here is my code

for react native side

import io from 'socket.io-client'; var socket = io.connect('http://localhost:8888',{transports: ['websocket'],upgrade:false}); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); });

   socket.on('connect_error', (err) => {
    console.log('error123',err)
  });

server side:

var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server);

server.listen(8888); // WARNING: app.listen(80) will NOT work here!

app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); });

io.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); });

index.html

WebScockets 101

I already stock need help

jingfreeks avatar Dec 07 '18 12:12 jingfreeks

Even I have the issue connecting the socket-io on android. The RN is able to get connected as the client however the same for the react web app but not react mobile app. Please suggest the solution.

PriyaJainDev avatar Dec 08 '18 18:12 PriyaJainDev

Try downgrading socket.io-client to 1.7.2, it works for me.

shubhsherl avatar Dec 11 '18 03:12 shubhsherl

I am having issues in connecting the websocket (socket.io-client, trnasport:['websocket']) on iOS with the webserver. Interestingly, it works when I turn on "Debug JS Remotely" on the device. This might be a problem with the react-native version on iOS. Just curious if someone is facing the same issue.

skumarcm avatar Jan 29 '19 20:01 skumarcm

For future readers:

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

Please reopen if needed.

darrachequesne avatar Apr 08 '24 16:04 darrachequesne