meteor-react-native icon indicating copy to clipboard operation
meteor-react-native copied to clipboard

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

Open nwabdou85 opened this issue 3 years ago • 3 comments

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;

nwabdou85 avatar Dec 20 '21 11:12 nwabdou85

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).

TheRealNate avatar Dec 20 '21 16:12 TheRealNate

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

nwabdou85 avatar Dec 21 '21 13:12 nwabdou85

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/>;
  }
}

TheRealNate avatar Jan 14 '22 07:01 TheRealNate

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

github-actions[bot] avatar Dec 02 '22 02:12 github-actions[bot]