Skip to content

Commit 87d9ca8

Browse files
committed
Add realtime client to flagsmith
1 parent 16536ea commit 87d9ca8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

lib/flagsmith.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require 'flagsmith/sdk/models/flags'
1717
require 'flagsmith/sdk/models/segments'
1818
require 'flagsmith/sdk/offline_handlers'
19+
require 'flagsmith/sdk/realtime_client'
1920

2021
require 'flagsmith/engine/core'
2122

@@ -46,6 +47,7 @@ class Client # rubocop:disable Metrics/ClassLength
4647
# :environment_key, :api_url, :custom_headers, :request_timeout_seconds, :enable_local_evaluation,
4748
# :environment_refresh_interval_seconds, :retries, :enable_analytics, :default_flag_handler,
4849
# :offline_mode, :offline_handler, :polling_manager_failure_limit
50+
# :realtime_api_url, :enable_realtime_updates, :logger
4951
#
5052
# You can see full description in the Flagsmith::Config
5153

@@ -59,6 +61,7 @@ def initialize(config)
5961
@identity_overrides_by_identifier = {}
6062

6163
validate_offline_mode!
64+
validate_realtime_mode!
6265

6366
api_client
6467
analytics_processor
@@ -78,10 +81,21 @@ def validate_offline_mode!
7881
'Cannot use offline_handler and default_flag_handler at the same time.'
7982
end
8083

84+
def validate_realtime_mode!
85+
return unless @config.realtime_mode? && !@config.local_evaluation?
86+
87+
raise Flagsmith::ClientError,
88+
'The enable_realtime_updates config param requires a matching enable_local_evaluation param.'
89+
end
90+
8191
def api_client
8292
@api_client ||= Flagsmith::ApiClient.new(@config)
8393
end
8494

95+
def realtime_client
96+
@realtime_client ||= Flagsmith::RealtimeClient.new(@config)
97+
end
98+
8599
def engine
86100
@engine ||= Flagsmith::Engine::Engine.new
87101
end
@@ -104,6 +118,14 @@ def load_offline_handler
104118
def environment_data_polling_manager
105119
return nil unless @config.local_evaluation?
106120

121+
# Bypass the environment data polling manager if realtime
122+
# is present in the configuration.
123+
if @config.realtime_mode?
124+
update_environment
125+
realtime_client.listen self unless realtime_client.running
126+
return
127+
end
128+
107129
update_environment if @environment_data_polling_manager.nil?
108130

109131
@environment_data_polling_manager ||= Flagsmith::EnvironmentDataPollingManager.new(

0 commit comments

Comments
 (0)