Everything works fine on iOS but on Android it doesn't connect Here's my code
import { useState, useEffect } from 'react'
import { io } from 'socket.io-client'
const URL = 'http://localhost:8001'
const socket = io(URL)
const useSocket = () => {
const [isConnected, setIsConnected] = useState(socket.connected)
useEffect(() => {
const onConnect = () => setIsConnected(true)
const onDisconnect = () => setIsConnected(false)
if (!socket.connected) {
socket.connect()
}
socket.on('connect', onConnect)
socket.on('disconnect', onDisconnect)
return () => {
socket.off('connect', onConnect)
socket.off('disconnect', onDisconnect)
}
}, [])
return { socket, isConnected }
}
export default useSocket
UPDATE:
It works if i replace
const URL = 'http://localhost:8001'
with
const URL = 'http://192.168.1.91:8001'
which is my local IP on the network
I'm going to leave the question open because it would be very useful to be able to just use localhost
or 127.0.0.1
instead of a specific IP