Below is my react native component and node server.js code that I am trying to connect to my node websocket backend.
My react code is running on the same computer as th
Your server code seems fine. Though you should check if you can connect to its socket via another client.
Anyway try this for react native code.
Import by:
import io from 'socket.io-client/socket.io'
In your component do:
componentDidMount () {
const socket = io(YOURURL, {
transports: ['websocket']
})
socket.on('connect', () => {
console.log("socket connected")
socket.emit('YOUR EVENT TO SERVER', {})
socket.on('EVENT YOU WANNA LISTEN', (r) => {
})
})
socket.on('connect_error', (err) => {
console.log(err)
})
socket.on('disconnect', () => {
console.log("Disconnected Socket!")
})
}