Configuring your Mobile Applications on the fly with Parse Config
Tags: parseserver, devops, tutorial, javascript
Published: 2019-10-29 12:53:28

If you have ever wanted to update the configuration of your app on the fly, you should know how frustrating it is that the little update would require a fresh app release. Fortunately, Parse Config is here to help! It's a very simple, yet at the same time, incredibly useful feature which allows you to add parameters to your app, which you can update whenever you want, without worrying that configuration changes will require a new release. It is stored as a configuration object on your Parse Server instance, so you can fetch it at any given time. So let's get to the point.

## Getting Started ##

It's fairly easy to start using Parse Config. In your SashiDo applications you can do that in **Dashboard -> Your App -> Core -> Config**. Afterward, you have to create the new parameters, which you'll be able to use and update whenever you want. 
To do that click on the green button **Create your first parameter** or the **Create a parameter** button in the top-right corner of the Dashboard.

![parseConfigNavigation](https://media-blog.sashido.io/content/images/2018/06/parseConfigNavigation.jpg)

Alright, so far so good. Now you'll need to specify a couple of things:
* **Parameter Name** - It can be anything, but we suggest that it's something informative in regards to what you'll use it for.

* **Type & Value** - Those two go hand-to-hand with each other. You may wonder what data type your parameters can be, but don't worry as Parse Config supports nearly any data type:
     * **Boolean** - This is the standard true/false data type. You can toggle if it's true or false with just one click! Red is false and green is true.
     * **String** - Any form of text for example "Hello from SashiDo's blog!"
     * **Number** - You can use this for both integer type numbers like 1; 2; 3; and for floating-point number e.g 2.6; 10.65; 1.05 etc.
     * **Date** - It's self-explanatory, a date format. You can even set whatever time you like to your date. When you choose this type a little calendar-like window appears when you click to set the value.
     * **Object** - Any object that contains key/value pairs.
     * **Array** - An array of items, for example, strings, numbers, objects or dates ["user1","user2","user3"]; [1,2,3,4]; [{“George”:true},{“Alexa”:false}]. As you can see they can be from any data type and even in JSON format.
     * **Geo Point** - With this you can specify coordinates to a location with given **Latitude** & **Longitude**.
     * **File** - Last but not least, your parameter could be a file. You simply need to upload it.
     
![createParameter](https://media-blog.sashido.io/content/images/2018/06/createParameter.gif)

If you want to edit your parameter, just hit the **Edit** button. In case you feel that you don't have any more need for it, just delete it by clicking the **Delete** button.

![editDeletePar](https://media-blog.sashido.io/content/images/2018/06/editDeletePar.jpg)

Take into consideration that you can have up to **100 different parameters** or a **total size of 128KB across all parameters** as described in the Official Parse Server JS SDK [here](https://docs.parseplatform.org/js/guide/#parameters).

That's it! Now you have created your parameter. All good, but now you may ask yourself, how do I fetch what I've created from my client?

### Retrieving Config ###

Let's say we've created a parameter **dailyMessage** of type **string** with a certain value, like "**Today's specialty is Pineapple Pizza!**". Let's assume that for our example we are building our application with JavaScript. You can simply retrieve your parameter like this:

```javascript
  Parse.Config.get().then(function(config) {
    var dailyMessage = config.get("dailyMessage");
    // Now display your message wherever you want!
}, function(error) {
    // Something went wrong (e.g. request timed out)
});

```
Note that `Parse.Config.get();` by itself will fetch all your parameters from the server. 

As you can see it's fairly simple! The cool part is that the JavaScript SDK, or any other as a matter of fact, automatically caches your last fetched Config, so you are not required to retrieve it again after the application has been restarted. How cool is that? Simply awesome! It's built to be as reliable as possible even if your internet connection is poor. You can always fetch your cached Config and assign it to a variable like in the example below:

```javascript
   var config = Parse.Config.current(); // Note that this will return all cached parameters.
   config.get("dailyMessage"); // Here you specify exactly which parameter’s value you want.
   
   // Now you can do something with it.
```
A great addition is that every Parse Config instance that you get is **immutable**. That means that if in the future you retrieve a new Parse Config from the server, it will not change or modify any existing Parse Config instances. It will lazily create a new instance and make it available through `Parse.Config.current()`. As you may have already guessed that means that you can safely use any **current()** object that you've created without worrying that it will be automatically updated.

If you want to read more about how to retrieve Config with the SDK of your choice, check these links to the Official Parse Docs:

* [How to Retrieving Config with the iOS SDK](https://docs.parseplatform.org/ios/guide/#retrieving-config)
* [Retrieving Config with the Android SDK](https://docs.parseplatform.org/android/guide/#retrieving-config)
* [Retrieving Config the .NET + Xamarine SDK](https://docs.parseplatform.org/dotnet/guide/#retrieving-config)
* [Retrieving Config with the MacOS SDK](https://docs.parseplatform.org/macos/guide/#retrieving-config)

### ProTip  ###

#### Save requests by retrieving Config only once in a while ####

As you may understand by now, it may be a little bit of trouble for you if you retrieve the Config every time you want to use it. If that's the case, you can always use the cached **current** object of your last fetched config. You can also implement a simple logic to retrieve the latest Config only once in while like this:

```javascript
   const getConfig = function() {
    let lastFetch;
    const timeBeforeFetch = 1000 * 60 * 60 * 24; // Re-fetch the config from the server only if the given time has passed. In this example, one day.
    return function() {
        const currentDate = new Date();
        if (lastFetch === undefined ||
            currentDate.getTime() - lastFetch.getTime() > timeBeforeFetch) {
            lastFetch = currentDate;
            return Parse.Config.get();
        } else {
            return Promise.resolve(Parse.Config.current());
        }
    };
}();

```

That way you can save up on the requests you are going to make if you fetch the Config each time you need to use it. Rather try to fetch it again and if the required time hasn’t expired fall back to the cached Configs.

After that you can retrieve your desired parameter like that:
```javascript
const config = await getConfig();
config.get(‘yourParameterName’);
```

## Other UseCases and Scenarios ##

So far you've learned how to create your parameters and retrieve them. However, it may still be hard for you to think of a way to use Config.

### Feature Beta Testing ###
A good example is if you want to add this cool new feature to your application. Yet you want some of your users to test it out, but not all of them. Okay, that's awesome, as you can use two parameters of the Config to accomplish it. 

For starters you can add a parameter of type **Array** called "**betaTestUsers**". In it you can add all the User IDs you would like the feature to be available for beta testing. After that, you can create a second parameter of type **Date**. You can call it whatever you want, let's say "**featureStart**. That way you can specify exactly at what date and time the feature should be made available to the given users. Cool, isn't it? You can edit your **betaTestUsers** to include or exclude testers at runtime without worrying that you need to re-deploy your application.

### Cooking app, Recipe of the Day ###

Let's gaze upon another example. Say you have an application for cooking all sorts of pizza. You can have all sorts of different recipes but you can have a little special section for **_Today's Special Recipe_**. 

Cool idea, don't you think? But with it comes the part where you would like to update that special recipe and for example a picture of how it would look like when done. You can imagine how frustrating it would be to re-deploy your app every day because of some little message and a picture, right? Well as Config supports both **File** and **String** parameter types you can achieve this quite easily without the need for a fresh release. Simply create a new parameter of type **String** and call it "**dailySpecialRecipe**" and add the description of your recipe there. After that, make another config of type **File** so you can upload whatever picture you like and change it whenever you wish.

## It's always better with a [video](https://www.youtube.com/embed/nqDajm7PGHk?list=PLk66IKYX7bjrBKSNwNZVpTjVswA0oq5dr) ##


## Final ##

That's about it! You've learned of a way to update your applications' configuration on the fly. All that's left to do is let your imagination loose and take that information to your advantage!