I am unable to connect to a phoenix socket/channel from React Native code using the Expo framework and the phoenix npm package.
Here is the code:
useEffect(() => {
const socket = new Socket("ws://localhost:4000/socket", {});
socket.onOpen((event) => console.log("Connected."));
socket.onError((event) => console.log("Cannot connect."));
socket.onClose((event) => console.log("Goodbye."));
socket.connect();
const channel = socket.channel("room:1234");
channel
.join()
.receive("ok", (resp) => {
console.log("Joined pomodoro channel", resp);
})
.receive("error", (resp) => {
console.log("Error in joining", resp);
});
}, []);
Executing this code just gets me in a loop of ‘cannot connect’ and ‘goodbye’. A similar issue was posted a while ago here: here. The solution they found does not work for me, which is to hardcode my IP address instead of using localhost. This all works on a regular React application on a web browser. I am out of ideas.
The development environment is Linux Mint and testing on an iPhone 13.
I appreciate any help.