socket.io-client: version 2.2 does not work with react native

Reverting to 2.1.1 fixes the problem.

I suspect it has something to do with the new cors management changes. Manually Setting origins does not seem to do anything.

It magically start working when launching the react native debugger.

Config

reconnection: true,
reconnectionDelay: 500,
jsonp: false,
reconnectionAttempts: Infinity,
transports: ['websocket']

Setup

  • OS: iOS
  • browser: react-native
  • socket.io version: 2.2.0

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 14
  • Comments: 34 (3 by maintainers)

Most upvoted comments

Nothing seems like to be working with react native 0.60

socket.io (unlike the WebSocket API) only accepts a HTTP(S) protocol URI for the client (see the docs here), but it does use the WS protocol behind the scenes.

So if you do io('http://192.168.0.107:8080/endpoint', ...) it should work!

Yep, I’m currently running socket.io-client@2.2.0 (which has pulled in engine.io-client@3.3.2) on React Native without issue.

It’s worth checking to make sure your engine.io-client is 3.3.2, as this contains the React Native fix for websockets. 😃

@benhjames thanks for the update!

I think the ws:// should work properly, but the /endpoint means you want to connect to the endpoint namespace (that’s the current implementation, as of v2.x). You may want to use:

this.socket = io('ws://192.168.0.107:8080', {
  path: '/endpoint',
  reconnection: true,
  reconnectionDelay: 500,
  jsonp: false,
  reconnectionAttempts: Infinity,
  transports: ['websocket']
});

That looks to me like the endpoint is incorrect - it should be a http or https protocol link, not a ws one. 👍

I tried with Expo SDK v34 and v35 (RN 0.59) and had to downgrade socket.io-client to 1.7.2 (yarn add socket.io-client@1.7.2) as other versions (2.1.1, 2.1.0, 2.2.0) did NOT work. I didn’t try 2.3.0, now that it works I don’t want troubles 😃

Please see the above answer!

It may be needed to connect to a non-localhost URL (i.e. your computer’s IP) within React Native: certainly in Android and possibly in iOS depending on which simulator is being used.

If that still doesn’t work, then it’s worth trying the transports: ['polling'] option to see if it really is the WebSocket transport in particular that’s not working, or if it’s a more general network connection problem.

That explains it then! Socket.io needs to be used on both the client and server to work. 👍

@benhjames @darrachequesne I have used socket.io-client@2.2.0 (which has pulled in engine.io-client@3.3.2) and also verify the changes in node_modules still, it’s not working for me.

`import io from ‘socket.io-client’;

export default class HomeScreen extends React.Component { constructor(props){ super(props); this.socket = io(‘ws://192.168.0.107:8080/endpoint’,{ reconnection: true, reconnectionDelay: 500, jsonp: false, reconnectionAttempts: Infinity, transports: [‘websocket’] }); this.socket.on(‘connect_error’, (err) => { console.log(err) });
} `

I am getting the error as below : Error: websocket error at WS.Transport.onError (transport.js:67) at WebSocket.ws.onerror (websocket.js:157) at WebSocket.dispatchEvent (event-target.js:172) at WebSocket.js:289 at RCTDeviceEventEmitter.emit (EventEmitter.js:190) at MessageQueue.__callFunction (MessageQueue.js:349) at MessageQueue.js:106 at MessageQueue.__guard (MessageQueue.js:297) at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:105) at debuggerWorker.js:72