Elixir

Getting started

1. Add configcat to your list of dependencies in mix.exs

def deps do
[
{:configcat, "~> 1.0.0"}
]
end

2. Add ConfigCat to your application Supervisor tree

def start(_type, _args) do
children = [
{ConfigCat, [sdk_key: "YOUR SDK KEY"]},
MyApp
]
opts = [strategy: :one_for_one, name: Simple.Supervisor]
Supervisor.start_link(children, opts)
end

3. Get your setting value

isMyAwesomeFeatureEnabled = ConfigCat.get_value("isMyAwesomeFeatureEnabled", false)
if isMyAwesomeFeatureEnabled do
do_the_new_thing()
else
do_the_old_thing()
end

Configuring the ConfigCat Client

ConfigCat Client is responsible for:

  • managing the communication between your application and ConfigCat servers.
  • caching your setting values and feature flags.
  • serving values quickly in a failsafe way.

{ConfigCat, options} returns a client with default options.

PropertiesDescription
sdk_keyREQUIRED. SDK Key to access your feature flags and configurations. Get it from ConfigCat Dashboard.
data_governanceDescribes the location of your feature flag and setting data within the ConfigCat CDN. This parameter needs to be in sync with your Data Governance preferences. Defaults to :global. More about Data Governance. Available options: :global, :eu_only.
cache_policyCachePolicy.auto/1, CachePolicy.lazy/1 and CachePolicy.manual/0. Defaults to: CachePolicy.auto/0 See See below for details.
cacheCaching module you want configcat to use. Defaults to: ConfigCat.InMemoryCache.
http_proxySpecify this option if you need to use a proxy server to access your ConfigCat settings. You can provide a simple URL, like https://my_proxy.example.com or include authentication information, like https://user:password@my_proxy.example.com/.
nameA unique identifier for this instance of ConfigCat. Defaults to ConfigCat. Must be provided if you need to run more than one instance of ConfigCat in the same application. If you provide a name, you must then pass that name to all of the API functions using the client option.

Anatomy of get_value()

ParametersDescription
keyREQUIRED. Setting-specific key. Set on ConfigCat Dashboard for each setting.
default_valueREQUIRED. This value will be returned in case of an error.
userOptional, ConfigCat.User Object. Essential when using Targeting. Read more about Targeting.
clientIf you are running multiple instances of ConfigCat, provide the client: :unique_name option, specifying the name you configured for the instance you want to access.
value = ConfigCat.get_value(
"keyOfMySetting", # Setting Key
false, # Default value
ConfigCat.User.new("435170f4-8a8b-4b67-a723-505ac7cdea92") # Optional User Object
);

User Object

The User Object is essential if you'd like to use ConfigCat's Targeting feature.

user_object = ConfigCat.User.new("435170f4-8a8b-4b67-a723-505ac7cdea92")
user_object = ConfigCat.User.new("john@example.com")
ParametersDescription
identifierREQUIRED. Unique identifier of a user in your application. Can be any value, even an email address.
emailOptional parameter for easier targeting rule definitions.
countryOptional parameter for easier targeting rule definitions.
customOptional Map for custom attributes of a user for advanced targeting rule definitions. e.g. User role, Subscription type.
user_object = ConfigCat.User.new("435170f4-8a8b-4b67-a723-505ac7cdea92", email: 'john@example', country: 'United Kingdom',
custom: %{SubscriptionType: 'Pro', UserRole: 'Admin'})

Polling Modes

The ConfigCat SDK supports 3 different polling mechanisms to acquire the setting values from ConfigCat. After latest setting values are downloaded, they are stored in the internal cache then all requests are served from there. With the following polling modes, you can customize the SDK to best fit to your application's lifecycle.

Auto polling (default)

The ConfigCat SDK downloads the latest values and stores them automatically every 60 seconds.

Use the poll_interval_seconds option parameter to change the polling interval.

{ConfigCat, [
sdk_key: "YOUR SDK KEY",
cache_policy: CachePolicy.auto(poll_interval_seconds: 60)
]},

Adding a callback to on_changed option parameter will get you notified about changes.

{ConfigCat, [
sdk_key: "YOUR SDK KEY",
cache_policy: CachePolicy.auto(on_changed: callback/0)
]}

Available options:

Option ParameterDescriptionDefault
poll_interval_secondsPolling interval.60
on_changedA 0-arity function to be called about configuration changes. Needs to run on a separate proccess. Any exceptions raised by on_changed are caught and logged.-

Lazy loading

When calling get_value() the ConfigCat SDK downloads the latest setting values if they are not present or expired in the cache. In this case the get_value() will return the setting value after the cache is updated.

Use cache_expiry_seconds option parameter to set cache lifetime.

{ConfigCat, [
sdk_key: "YOUR SDK KEY",
cache_policy: CachePolicy.lazy(cache_expiry_seconds: 300)
]}

Available options:

Option ParameterDescriptionDefault
cache_expiry_secondsCache TTL.60

Manual polling

Manual polling gives you full control over when the setting values are downloaded. ConfigCat SDK will not update them automatically. Calling force_refresh() is your application's responsibility.

ConfigCat.force_refresh();

get_value() returns default_value if the cache is empty. Call force_refresh() to update the cache.

value = ConfigCat.get_value("key", "my default value") # Returns "my default value"
ConfigCat.force_refresh();
value = ConfigCat.get_value("key", "my default value") # Returns "value from server"

Custom cache behaviour with cache: option parameter

To be able to customize the caching layer you need to implement the following behaviour:

defmodule ConfigCat.ConfigCache do
alias ConfigCat.Config
@type key :: String.t()
@type result :: {:ok, Config.t()} | {:error, :not_found}
@callback get(key) :: {:ok, Config.t()} | {:error, :not_found}
@callback set(key, Config.t()) :: :ok
end
  • You must implement (either explicitly or implicitly) the ConfigCache behaviour
  • It is the responsibility of the calling application to supervise the cache if it needs to be supervised.

Multiple ConfigCat instances

If you need to run more than one instance of ConfigCat, you can add multiple ConfigCat children. You will need to give ConfigCat a unique name option for each, as well as using Supervisor.child_spec/2 to provide a unique id for each instance.

# lib/my_app/application.ex
def start(_type, _args) do
children = [
# ... other children ...
Supervisor.child_spec({ConfigCat, [sdk_key: "sdk_key_1", name: :first]}),
Supervisor.child_spec({ConfigCat, [sdk_key: "sdk_key_2", name: :second]}),
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end

Then you can call .get_value/4 like this:

ConfigCat.get_value("setting", "default", client: :first)
ConfigCat.get_value("setting", "default", client: :second)

Logging

In the ConfigCat SDK, we use the default Elixir's Logger so you can customise as you like.

[info] Fetching configuration from ConfigCat
[info] ConfigCat configuration json fetch response code: 200 Cached: "xxxxx-xxx"
[debug] Evaluating get_value('show_contact_phone_number').

get_all_keys()

You can query the keys from your configuration in the SDK with the get_all_keys() method.

keys = ConfigCat.get_all_keys()

Using ConfigCat behind a proxy

Provide your own network credentials (username/password), and proxy server settings (proxy server/port) by passing the proxy details to the creator method.

{ConfigCat, [
sdk_key: "YOUR SDK KEY",
http_proxy: "https://user@pass:yourproxy.com"
]}

Sample Applications

Look under the hood