meteorrn / meteor-react-native

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

Is it possible to serve a variable as Meteor.connect(VARIABLE_URL) value ? #80

Closed nwabdou85 closed 1 year ago

nwabdou85 commented 2 years ago

Hi,

To fix a problem of performance, I plan to implemente the load balancing and auto scalling options of AWS;

How to serve SERVER_URL as variable depending of the server available to the user ?

Ps: ans how decide which server should use (SO which SERVER_URL);

Thank's for your help;

TheRealNate commented 2 years ago

Hi @nwabdou85,

Yes, it is possible to use a variable when calling Meteor.connect. The only thing you’ll need to keep in mind is that if you call any Meteor.* APIs before calling Meteor.connect the app will likely crash. This means you’ll need to prevent rendering of your app until you’ve determined the URL and called Meteor.connect, and you also need to make sure that you don’t use any Meteor.* APIs anywhere in the root of any files (as these will get called immediately upon importing).

nwabdou85 commented 2 years ago

Thank @TheRealNate in this case, how can I return the url from load balancer ?

TheRealNate commented 2 years ago

Hey @nwabdou85,

Sorry for the extremely late reply! In order to do this there are two things you need to ensure:

First, do not call any Meteor functions in your root code (root code means code that is not contained inside a function)

Root code (bad):

import foo from 'bar';

Meteor.call("abc");

Not root code (good):

import foo from 'bar';

const myOnConnectFunction = () => {
  Meteor.call("abc");
}

Second, you will need to ensure that no Meteor functions are called before you call Meteor.connect. Its important to clarify, you can call functions before the connection to the server has been established (the package handles that internally), just not before you actually call the Meteor.connect(...) function.

In my opinion, the best way to ensure this is to have a container class that retrieves your URL and does not render the rest of your app before then. Here is an example. Please note I wrote this code directly into my reply (I did not test run it). Use it as an example when writing your own code if you'd like.

const MyLoadingWrapper = () => {
  const [isReady, setReady] = React.useState(false);

  React.useEffect(() => {
    const url = getMyURLFromLoadBalancer();
    Meteor.connect(url);
    setReady(true);
  }, []);

  if(isReady) {
    return <MyComponent/>;
  }
  else {
    return <LoadingScreen/>;
  }
}
github-actions[bot] commented 1 year ago

Closing this issue due to no activity. Feel free to reopen.