meteorrn / meteor-react-native

Meteor client for React Native matching Meteor Spec
https://guide.meteor.com/react-native.html
Other
58 stars 31 forks source link

how to connect to localhost Meteor app #82

Closed jankapunkt closed 2 years ago

jankapunkt commented 2 years ago

I am using expo and I am trying to connect to a Meteor app on localhost in dev mode (expo start; bundle is pushed to my real android phone, no emulator)

Meteor.connect('ws://localhost:8080/websocket')
Meteor.getData().ddp.socket.rawSocket.onerror = e => console.error(e);
Meteor.getData().ddp.socket.addListener("open", () => console.log("open"));
Meteor.getData().ddp.socket.addListener("close", () => console.log("close"));

which produces the following output:

close
close

Event {
  "isTrusted": false,
  "message": "Failed to connect to localhost/127.0.0.1:8080",
}
at meteor/connect.js:12:2 in connectMeteor
at node_modules/react-native/Libraries/WebSocket/WebSocket.js:258:8 in _eventEmitter.addListener$argument_1
at node_modules/react-native/Libraries/vendor/emitter/_EventEmitter.js:135:10 in EventEmitter#emit

Dependencies:

    "@babel/preset-react": "^7.16.0",
    "@expo/vector-icons": "^12.0.5",
    "@meteorrn/core": "^2.3.0",
    "@react-native-community/netinfo": "^7.1.7",
    "@react-navigation/native": "^6.0.6",
    "@react-navigation/native-stack": "^6.2.5",
    "@react-navigation/stack": "^6.0.11",
    "babel-preset-es2015": "^6.24.1",
    "colors": "1.4.0",
    "expo": "~43.0.3",
    "expo-app-loading": "^1.2.1",
    "expo-font": "~10.0.3",
    "expo-speech": "~10.0.3",
    "expo-splash-screen": "~0.13.5",
    "i18next": "^21.5.4",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-i18next": "^11.14.3",
    "react-native": "^0.64.3",
    "react-native-elements": "^3.4.2",
    "react-native-gesture-handler": "^1.10.3",
    "react-native-safe-area-context": "3.3.2",
    "react-native-screens": "~3.8.0",
    "react-native-tts": "^4.1.0",
    "react-native-vector-icons": "^9.0.0",
    "react-native-web": "~0.17.5"

Is this expected; did I do something wrong here?

TheRealNate commented 2 years ago

Hey @jankapunkt, if you're pushing the bundle to a physical device localhost will not be accessible as localhost is only available on the same device the Meteor app is being run from. Your best bet is to locate the LAN IP address (https://en.wikipedia.org/wiki/Private_network#Private_IPv4_addresses) for your machine running Meteor and connect to that. For example: ws://192.168.1.123:8080/websocket (that IP address is completely made up, you'll need to find the correct one for your machine).

Here is a StackOverflow question on this: https://stackoverflow.com/questions/47417766/calling-locally-hosted-server-from-expo-app. They have a different route where they dynamically access the IP address of your machine. It is up to you which option you prefer.

I'm removing the bug tag as I don't believe at this time that this is related to an issue with this package, however I will leave the issue open for now. Please close if you're able to solve your problem.

jankapunkt commented 2 years ago

Hi thank you yes that fixed it. I would add this to the documentation as a PR so others won't jump into that trap, what do you think?

TheRealNate commented 2 years ago

@jankapunkt absolutely agree. I'll add some more details soon but for now I've added a link to this issue in the README under Basic Usage. I'll also close this issue.