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.combut 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.
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/pubnub/client.rb', line 184 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.
97 98 99 |
# File 'lib/pubnub/client.rb', line 97 def env @env end |
#subscriber ⇒ Object (readonly)
Returns the value of attribute subscriber.
97 98 99 |
# File 'lib/pubnub/client.rb', line 97 def subscriber @subscriber end |
#telemetry ⇒ Object (readonly)
Returns the value of attribute telemetry.
97 98 99 |
# File 'lib/pubnub/client.rb', line 97 def telemetry @telemetry end |
Instance Method Details
#add_listener(options) ⇒ Object
197 198 199 |
# File 'lib/pubnub/client.rb', line 197 def add_listener() @subscriber.add_listener() end |
#apply_state(event) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/pubnub/client.rb', line 306 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.
351 352 353 |
# File 'lib/pubnub/client.rb', line 351 def crypto_module @env[:crypto_module] end |
#empty_state? ⇒ Boolean
321 322 323 324 |
# File 'lib/pubnub/client.rb', line 321 def empty_state? return true unless @env[:state] totally_empty @env[:state] end |
#generate_ortt ⇒ Object
326 327 328 |
# File 'lib/pubnub/client.rb', line 326 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.
292 293 294 295 296 297 298 299 |
# File 'lib/pubnub/client.rb', line 292 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 Pubnub.logger.debug("Pubnub::Client") { "There's no requester" } end |
#parse_token(token) ⇒ Object
338 339 340 341 |
# File 'lib/pubnub/client.rb', line 338 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
330 331 332 |
# File 'lib/pubnub/client.rb', line 330 def record_telemetry(telemetry_type, time_start, time_end) @telemetry.async.record_request(telemetry_type, time_start, time_end) end |
#remove_listener(options) ⇒ Object
201 202 203 |
# File 'lib/pubnub/client.rb', line 201 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.
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/pubnub/client.rb', line 263 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
301 302 303 304 |
# File 'lib/pubnub/client.rb', line 301 def sequence_number_for_publish! @env[:sequence_number_for_publish] += 1 @env[:sequence_number_for_publish] % 2**32 end |
#set_token(token) ⇒ Object
343 344 345 |
# File 'lib/pubnub/client.rb', line 343 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.
216 217 218 219 220 221 222 |
# File 'lib/pubnub/client.rb', line 216 def subscribed? if @subscriber.nil? false else ![@subscriber.channels, @subscriber.groups, @subscriber.wildcard_channels].flatten.empty? end end |
#subscribed_channels ⇒ Object
205 206 207 |
# File 'lib/pubnub/client.rb', line 205 def subscribed_channels @subscriber.channels + @subscriber.wildcard_channels end |
#subscribed_groups ⇒ Object
209 210 211 |
# File 'lib/pubnub/client.rb', line 209 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.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/pubnub/client.rb', line 227 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
334 335 336 |
# File 'lib/pubnub/client.rb', line 334 def telemetry_for(event) @telemetry.await.fetch_average(event).value end |