question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't connect via socket.io-client on react-native

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:40 (3 by maintainers)

github_iconTop GitHub Comments

52reactions
rastapastacommented, Sep 26, 2019

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 to https://foo.bar/socket.io without 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:

  • use socket.io-client version 2.1.1
// package.json
{
  "dependencies": {
     "socket.io-client": "2.1.1"
   }
}
  • when setting up the client, define a host and a path
const socket = socketIO('https://your.host.com/', {
  path: '/path/to/socket.io/'
})
  • and suddenly everything works as smooth as planed

Hope to have saved you the time I spent on this issue 😆

5reactions
rxhivzerocommented, Nov 30, 2018

Try “socket.io-client”: “2.0.4” This may be socket.io bug

Read more comments on GitHub >

github_iconTop Results From Across the Web

Socket.io client is not working with React Native - Stack Overflow
I am using https://github.com/mrniko/netty-socketio in the backend. React Native app has been created using expo-cli. reactjs · sockets · react- ...
Read more >
How to Get Socket.io to work with React-Native - Brent Marquez
Connect the socket with .connect() and and add a listener to confirm the connection with a console.log. Note: initialize socketIO in ...
Read more >
Introduction | Socket.IO
Introduction. What Socket.IO is​. Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a ...
Read more >
Building a chat app with Socket.io and React Native
Here, I will guide you through creating the Socket.io Node.js server for real-time communication with the React Native application. Create a ...
Read more >
Socket.io client React hook for React Native/React - YouTube
In this video I'll show my implementation of a custom hook I created for dealing with livechat on the web/mobile. Since I'm using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found