Class: Pubnub::Client
- Includes:
- Events, GettersSetters, Helpers, PagedHistory, Configuration
- Defined in:
- lib/pubnub/client.rb,
lib/pubnub/client/events.rb,
lib/pubnub/client/helpers.rb,
lib/pubnub/client/paged_history.rb,
lib/pubnub/client/getters_setters.rb
Overview
Pubnub client Class
Defined Under Namespace
Modules: Events, GettersSetters, Helpers, PagedHistory
Constant Summary collapse
Constants included from Events
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#subscriber ⇒ Object
readonly
Returns the value of attribute subscriber.
-
#telemetry ⇒ Object
readonly
Returns the value of attribute telemetry.
Instance Method Summary collapse
- #add_listener(options) ⇒ Object
- #apply_state(event) ⇒ Object
-
#crypto_module ⇒ Pubnub::Crypto::CryptoProvider?
Data processing crypto module.
- #empty_state? ⇒ Boolean
- #generate_ortt ⇒ Object
-
#initialize(options) ⇒ Client
constructor
Parameters: ===========.
-
#kill_request_dispatcher(origin, event_type) ⇒ Object
Parameters: ===========
- origin
- Domain name where connection should be connected. .
- #parse_token(token) ⇒ Object
- #record_telemetry(telemetry_type, time_start, time_end) ⇒ Object
- #remove_listener(options) ⇒ Object
-
#request_dispatcher(origin, event_type, sync) ⇒ Object
Parameters: ===========
- origin
- Domain name where connection should be connected. .
- #sequence_number_for_publish! ⇒ Object
- #set_token(token) ⇒ Object
-
#subscribed? ⇒ Boolean
Returns: ======== True if client is subscribed to at least one channel or channel group, otherwise false.
- #subscribed_channels ⇒ Object
- #subscribed_groups ⇒ Object
-
#subscribed_to(separate_wildcard = false) ⇒ Object
Returns: ======== Hash with two keys: :channel and :group, representing currently subscribed channels and groups.
- #telemetry_for(event) ⇒ Object
Methods included from GettersSetters
#change_user_id, #change_uuid, #current_heartbeat, #current_origin, #events, #heartbeat=, #origin=, #region_code, #region_code=, #sdk_version, #subscribe_filter, #subscribe_filter=, #timetoken, #timetoken=, #user_id, #uuid
Methods included from PagedHistory
#all_history_messages, #paged_history
Methods included from Events
Constructor Details
#initialize(options) ⇒ Client
Parameters:
- subscribe_key
- required. Your subscribe key.
- publish_key
- optional. Your publish key, without it you can't push messages.
- secret_key
- optional. Your secret key, required for PAM operations.
- auth_key
- optional. This client auth key.
- cipher_key
- optional. Required to encrypt messages.
- uuid
- optional. Deprecated. Sets given uuid as client uuid, does not generates random uuid on init as usually
- user_id
- required. Sets given user_id as client user_id.
- origin
- optional. Specifies the fully qualified domain name of the PubNub origin.
By default this value is set to
pubsub.pubnub.com
but it should be set to the appropriate origin specified in the PubNub Admin Portal. - callback
- optional. Default callback function for all events if not overwrote while firing event.
- ssl
- optional. Your connection will use ssl if set to true.
- random_iv
- optional. Whether data should be encrypted / decrypted using random initialization vector.
- heartbeat
- optional. Heartbeat interval, if not set heartbeat will not be running.
- subscribe_timeout
- optional, be careful when modifying this. Timeout for subscribe connection in seconds.
- non_subscribe_timeout
- optional, be careful when modifying this. Timeout for non-subscribe connection in seconds.
- max_retries
- optional. How many times client should try to reestablish connection before fail.
- ttl
- optional. Default ttl for grant and revoke events.
examples:
# Minimal initialize
pubnub = Pubnub.new(subscribe_key: :my_sub_key)
# More complex initialize
pubnub = Pubnub.new(
subscribe_key: :demo,
publish_key: :demo,
secret_key: :secret,
cipher_key: :other_secret,
user_id: :mad_max,
origin: 'custom.pubnub.com',
callback: ->(envelope) { puts envelope. },
connect_callback: ->() { puts },
heartbeat: 60,
subscribe_timeout: 310,
non_subscribe_timeout: 10,
max_retries: 10,
ttl: 0
)
Returns:
Initialized Pubnub::Client ready to use.
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/pubnub/client.rb', line 183 def initialize() env_hash = () setup_app env_hash clean_env prepare_env validate! @env setup_crypto_module @telemetry = Telemetry.new Pubnub.logger.debug('Pubnub::Client') do "Created new Pubnub::Client instance. Version: #{Pubnub::VERSION}" end end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
96 97 98 |
# File 'lib/pubnub/client.rb', line 96 def env @env end |
#subscriber ⇒ Object (readonly)
Returns the value of attribute subscriber.
96 97 98 |
# File 'lib/pubnub/client.rb', line 96 def subscriber @subscriber end |
#telemetry ⇒ Object (readonly)
Returns the value of attribute telemetry.
96 97 98 |
# File 'lib/pubnub/client.rb', line 96 def telemetry @telemetry end |
Instance Method Details
#add_listener(options) ⇒ Object
196 197 198 |
# File 'lib/pubnub/client.rb', line 196 def add_listener() @subscriber.add_listener() end |
#apply_state(event) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/pubnub/client.rb', line 305 def apply_state(event) Pubnub.logger.debug('Pubnub::Client') { 'Apply state' } create_state_pools(event) return unless event.state event.channel.each do |channel| @env[:state][event.origin][:channel][channel] = event.state end event.group.each do |group| @env[:state][event.origin][:group][group] = event.state end end |
#crypto_module ⇒ Pubnub::Crypto::CryptoProvider?
Data processing crypto module.
350 351 352 |
# File 'lib/pubnub/client.rb', line 350 def crypto_module @env[:crypto_module] end |
#empty_state? ⇒ Boolean
320 321 322 323 |
# File 'lib/pubnub/client.rb', line 320 def empty_state? return true unless @env[:state] totally_empty @env[:state] end |
#generate_ortt ⇒ Object
325 326 327 |
# File 'lib/pubnub/client.rb', line 325 def generate_ortt (::Time.now.to_f * 10_000_000).to_i end |
#kill_request_dispatcher(origin, event_type) ⇒ Object
Parameters:
- origin
- Domain name where connection should be connected.
- event_type
- Keyword. :subscribe_event or :single_event.
Functionality:
Terminates request dispatcher for given origin and event type. Usable while restarting subscription.
291 292 293 294 295 296 297 298 |
# File 'lib/pubnub/client.rb', line 291 def kill_request_dispatcher(origin, event_type) Pubnub.logger.debug('Pubnub::Client') { 'Killing requester' } # @env[:req_dispatchers_pool][origin][event_type].async.terminate @env[:req_dispatchers_pool][:async][origin][event_type].reset_all @env[:req_dispatchers_pool][:async][origin][event_type] = nil rescue StandardError Pubnub.logger.debug('Pubnub::Client') { 'There\'s no requester' } end |
#parse_token(token) ⇒ Object
337 338 339 340 |
# File 'lib/pubnub/client.rb', line 337 def parse_token(token) token_bytes = Base64.urlsafe_decode64(token) Cbor.new.decode(token_bytes.bytes) end |
#record_telemetry(telemetry_type, time_start, time_end) ⇒ Object
329 330 331 |
# File 'lib/pubnub/client.rb', line 329 def record_telemetry(telemetry_type, time_start, time_end) @telemetry.async.record_request(telemetry_type, time_start, time_end) end |
#remove_listener(options) ⇒ Object
200 201 202 |
# File 'lib/pubnub/client.rb', line 200 def remove_listener() @subscriber.remove_listener() end |
#request_dispatcher(origin, event_type, sync) ⇒ Object
Parameters:
- origin
- Domain name where connection should be connected.
- event_type
- Keyword. :subscribe_event or :single_event.
- sync
- Boolean. True if we want dispatcher for sync or sync event, otherwise false.
Returns:
Appropriate RequestDispatcher.
It returns always new RequestDispatcher for sync events. For async events it checks if there's already RequestDispatcher created and returns it if created, otherwise creates it, assigns it in @env and returns newly created dispatcher.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/pubnub/client.rb', line 262 def request_dispatcher(origin, event_type, sync) Pubnub.logger.debug('Pubnub::Client') do "Looking for requester for #{sync ? 'sync' : 'async'} #{event_type}" end if sync @env[:req_dispatchers_pool][:sync][origin] ||= {} @env[:req_dispatchers_pool][:sync][origin][event_type] ||= setup_httpclient(event_type) else @env[:req_dispatchers_pool][:async][origin] ||= {} @env[:req_dispatchers_pool][:async][origin][event_type] ||= setup_httpclient(event_type) end end |
#sequence_number_for_publish! ⇒ Object
300 301 302 303 |
# File 'lib/pubnub/client.rb', line 300 def sequence_number_for_publish! @env[:sequence_number_for_publish] += 1 @env[:sequence_number_for_publish] % 2 ** 32 end |
#set_token(token) ⇒ Object
342 343 344 |
# File 'lib/pubnub/client.rb', line 342 def set_token(token) @env[:token] = token end |
#subscribed? ⇒ Boolean
Returns:
True if client is subscribed to at least one channel or channel group, otherwise false.
215 216 217 218 219 220 221 |
# File 'lib/pubnub/client.rb', line 215 def subscribed? if @subscriber.nil? false else ![@subscriber.channels, @subscriber.groups, @subscriber.wildcard_channels].flatten.empty? end end |
#subscribed_channels ⇒ Object
204 205 206 |
# File 'lib/pubnub/client.rb', line 204 def subscribed_channels @subscriber.channels + @subscriber.wildcard_channels end |
#subscribed_groups ⇒ Object
208 209 210 |
# File 'lib/pubnub/client.rb', line 208 def subscribed_groups @subscriber.groups end |
#subscribed_to(separate_wildcard = false) ⇒ Object
Returns:
Hash with two keys: :channel and :group, representing currently subscribed channels and groups.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/pubnub/client.rb', line 226 def subscribed_to(separate_wildcard = false) if separate_wildcard { channel: @subscriber.channels, group: @subscriber.groups, wildcard_channel: @subscriber.wildcard_channels } else { channel: @subscriber.channels + @subscriber.wildcard_channels, group: @subscriber.groups } end end |
#telemetry_for(event) ⇒ Object
333 334 335 |
# File 'lib/pubnub/client.rb', line 333 def telemetry_for(event) @telemetry.await.fetch_average(event).value end |