inProgress-team / react-native-meteor

Meteor Reactivity for your React Native application :)
MIT License
693 stars 211 forks source link

[Question] trouble with connecting android to local meteor app in a same WIFI network #175

Open cyclops24 opened 7 years ago

cyclops24 commented 7 years ago

Hi guys, I can connect successfully to a deployed Meteor app (on a VPS) and ddp but when I try to connect to a localhost with this url const url = 'ws://localhost:3000/websocket'; or const url = 'ws://127.0.0.1:3000/websocket'; it's not connect and app in waiting in spinner (see below code) I also try my ifconfig output IP address like const url = 'ws://192.168.1.102:3000/websocket'; but it's still not working. Can you guide me about this?

This is my landing page code that app waiting there. LoginContainer for reactive parts:

import React, { Component } from 'react';
import Meteor, { createContainer } from 'react-native-meteor';
import LoginComponent from '../routes/login';

export default LoginContainer = createContainer(() => {
  return {
    status: Meteor.status(),
    loggingIn: Meteor.loggingIn(),
    user: Meteor.user(),
    userId: Meteor.userId()
  }
}, LoginComponent);

And this is my LoginComponent for login form:

import React, { Component, PropTypes } from 'react';
import { Container, Content, Button, Text, List, Icon,
    ListItem, InputGroup, Input, Item, Spinner } from 'native-base';
import Meteor from 'react-native-meteor';
import { Actions } from 'react-native-router-flux';

export default class LoginComponent extends Component {

    constructor(props) {
        super(props);
        this.state = {
            username: '',
            password: '',
            loginError: ''
        };

        this.handleClick = this.handleClick.bind(this);
        this.handleUsernameChange = this.handleUsernameChange.bind(this);
        this.handlePasswordChange = this.handlePasswordChange.bind(this);
    }

    handleClick() {
        // handle login here
    };

    handleUsernameChange(text) {
        this.setState({username: text});
    };

    handlePasswordChange(text) {
        this.setState({password: text});
    };

  render() {
      if (!this.props.status.connected) {
          return (
              <Container>
                  <Content>
                      <Spinner />
                  </Content>
              </Container>
          );
      }
      return (
          <Container>
              <Content>
                  // my login form here
              </Content>
          </Container>
      );

  }
}

LoginComponent.propTypes = {
    status: PropTypes.object,
    loggingIn: PropTypes.bool.isRequired,
    user: PropTypes.object,
    userId: PropTypes.string
};

/CC: @noris666

noris666 commented 7 years ago

@cyclops24 you test on device or on emulator ? You also connected in that LAN where VPS ? I need described understanding of your environment.

cyclops24 commented 7 years ago

@noris666 Thanks for your rapid answer man. 😉 I'm using an android device. I have a VPS with a dedicated IP (its accessible through internet network and my device also connected to internet) and can connect to it successfully, But when I try connecting localhost it's not connect and waiting.

If it's not enough tell me which info needs.

noris666 commented 7 years ago

@cyclops24 ok I understand all. So i think if you want connect to localhost, first you need to connect your computer and phone to one LAN. Then check your firewall on computer, port 3000 opened ?

Because i also have like you environment and all works.

cyclops24 commented 7 years ago

Thanks @noris666, My computer and phone now connected to one LAN. But I didn't any specific config for my firewall. I'm using linux Mint (based on Ubuntu). Is it needs for me?

noris666 commented 7 years ago

@cyclops24 to example maybe you have filtered ports. You can check by telnet that. To example command: telnet yourip 3000

simonbelanger commented 7 years ago

@cyclops24 you probably have this behavior because because Meteor.status() is not a reactive data source (see #170 )

cyclops24 commented 7 years ago

@simonbelanger thanks for your attention. But I think Meteor.status() is reactive because it's work as a reactive data source when I use with deployed app url.

cyclops24 commented 7 years ago

@simonbelanger Meteor.status() also mentions here in ReadMe.md reactive-variables section: https://github.com/inProgress-team/react-native-meteor#reactive-variables

simonbelanger commented 7 years ago

@cyclops24 interesting... it'll need more investigation

cyclops24 commented 7 years ago

@noris666 I didn't find any setting for firewall and port in my Mint. Do you have any suggestion. Can you tell me your device setting or steps?

cyclops24 commented 7 years ago

Guys I still can't run my app when run Meteor app locally. I also have same issue when try running example app.

noris666 commented 7 years ago

@cyclops24 which version of android you use ? adb reverse supports from Android 5+

cyclops24 commented 7 years ago

@noris666 oh man I used android 4.x. I'm test it with my other device with android 5.x and say result here.

fakenickels commented 7 years ago

@cyclops24 have you tried running $ adb reverse tcp:3000 tcp:3000? So adb will make localhost:3000 on your device connect to your machine :3000.

charlesdeb commented 7 years ago

For others struggling to get access to the local Mongo database and meteor server on Windows 10, first run ipconfig to get the different ip addresses of your local machine, and then:

GenyMotion Android Emulator

Use the IP address of the VirtualBox host-only. I had to use the second one of the two addresses provided. My connection string was: ws://192.168.236.2:3000/websocket

Real Android device

Use the IP address of your machine on the LAN - either the WiFi or ethernet adapter. My connection string was: ws://192.168.0.24:3000/websocket

In neither case was I able to successfully use the wss protocol in my install.

sajibcse68 commented 6 years ago

It works for me: ifconfig find the innet IP of en0: e.g. 192.168. .

Using Android Emulator Set the server URL in react app like:

SERVER_URL: 'ws://192.168.*.*:3000/websocket',
alexismoreau commented 6 years ago

You should use Meteor.connect('ws://127.0.0.1:3000/websocket'); to connect to local server.