I have been unsuccessful in trying to make a WebSocket connection between a React Native app client (running in Expo) and a NodeJs server, using the 'ws' websocket library on both sides.
I have set up the WebSocket server, which I can successfully connect to using a chrome extension websocket client, after entering the ip address and port e.g. 'ws://192.168.208.1:8080'.
I have set up the client code within the app but it cannot connect to the server! However I am able to connect to and send messages to the Websocket echo url 'wss://echo.websocket.org/' with no problem!
So the code is working correctly on both sides in the test cases, it seems like there is a problem making that remote connection.
Sanity checks 1. Both devices are on the same wi-fi network 2. I have tried connecting after disabling firewall
CLIENT CODE:
// THIS DOESN'T WORK!
// this.ws = new WebSocket('ws://192.168.208.1:8080/');
// THIS DOES WORK!
// this.ws = new WebSocket('wss://echo.websocket.org/');
this.ws.onopen = (evt) => console.log('connected');
this.ws.addEventListener('open', () => {
this.ws.send(JSON.stringify('Hello!'));
});
this.ws.addEventListener('message', event => {
console.log('Received:', event.data);
});