Class: Pubnub::Client
- Includes:
- Connections, Events, Helpers, PagedHistory, Configuration
- Defined in:
- lib/pubnub/client.rb,
lib/pubnub/client/events.rb,
lib/pubnub/client/helpers.rb,
lib/pubnub/client/connections.rb,
lib/pubnub/client/paged_history.rb
Overview
Pubnub client Class
Defined Under Namespace
Modules: Connections, Events, Helpers, PagedHistory
Constant Summary collapse
Constants included from Events
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#heart ⇒ Object
readonly
Returns the value of attribute heart.
-
#subscriber ⇒ Object
readonly
Returns the value of attribute subscriber.
Instance Method Summary collapse
- #add_listener(options) ⇒ Object
- #apply_state(event) ⇒ Object
-
#change_uuid(uuid) ⇒ Object
(also: #session_uuid=, #uuid=, #set_uuid=)
Parameters: ===========
- uuid
- New uuid to be set.
- #current_heartbeat ⇒ Object
-
#current_origin ⇒ Object
(also: #origin)
Returns: ======== Current origin.
- #empty_state? ⇒ Boolean
-
#events ⇒ Object
Returns: ======== Array of all current events.
- #generate_ortt ⇒ Object
- #heartbeat=(value) ⇒ Object
-
#initialize(options) ⇒ Client
constructor
Parameters: ===========.
-
#kill_request_dispatcher(origin, event_type) ⇒ Object
Parameters: ===========
- origin
- Domain name where connection should be connected. .
-
#region_code ⇒ Object
Retruns: ======== Current region or default '0'.
-
#region_code=(region) ⇒ Object
Parameters: ===========
- region
- New region.
- #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
:nocov:.
- #subscribe_filter ⇒ Object
- #subscribe_filter=(filter_expr) ⇒ 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.
-
#timetoken ⇒ Object
Returns: ======== Current client timetoken.
-
#timetoken=(timetoken) ⇒ Object
Parameters: ===========
- timetoken
- New timetoken.
-
#uuid ⇒ Object
Returns: ======== Current uuid.
Methods included from PagedHistory
Methods included from Events
Methods included from Connections
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. Sets given uuid as client uuid, does not generates random uuid on init as usually.
- 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.
- 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,
uuid: :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.
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/pubnub/client.rb', line 157 def initialize() env_hash = () setup_app env_hash clean_env prepare_env validate! @env Pubnub.logger.info('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.
76 77 78 |
# File 'lib/pubnub/client.rb', line 76 def env @env end |
#heart ⇒ Object (readonly)
Returns the value of attribute heart.
76 77 78 |
# File 'lib/pubnub/client.rb', line 76 def heart @heart end |
#subscriber ⇒ Object (readonly)
Returns the value of attribute subscriber.
76 77 78 |
# File 'lib/pubnub/client.rb', line 76 def subscriber @subscriber end |
Instance Method Details
#add_listener(options) ⇒ Object
168 169 170 |
# File 'lib/pubnub/client.rb', line 168 def add_listener() @subscriber.add_listener() end |
#apply_state(event) ⇒ Object
375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/pubnub/client.rb', line 375 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 |
#change_uuid(uuid) ⇒ Object Also known as: session_uuid=, uuid=, set_uuid=
Parameters:
- uuid
- New uuid to be set.
Returns:
New uuid.
Functionality:
Can't change uuid while subscribed. You have to leave every subscribed channel.
286 287 288 289 290 291 292 293 |
# File 'lib/pubnub/client.rb', line 286 def change_uuid(uuid) Pubnub.logger.debug('Pubnub::Client') { 'Changing uuid' } if subscribed? fail('Cannot change UUID while subscribed.') else @env[:uuid] = uuid end end |
#current_heartbeat ⇒ Object
367 368 369 |
# File 'lib/pubnub/client.rb', line 367 def current_heartbeat @env[:heartbeat].to_i end |
#current_origin ⇒ Object Also known as: origin
Returns:
Current origin.
301 302 303 |
# File 'lib/pubnub/client.rb', line 301 def current_origin @env[:origins_pool].first end |
#empty_state? ⇒ Boolean
390 391 392 393 |
# File 'lib/pubnub/client.rb', line 390 def empty_state? return true unless @env[:state] totally_empty @env[:state] end |
#events ⇒ Object
Returns:
Array of all current events. :nocov:
357 358 359 |
# File 'lib/pubnub/client.rb', line 357 def events @env[:events] end |
#generate_ortt ⇒ Object
395 396 397 |
# File 'lib/pubnub/client.rb', line 395 def generate_ortt (::Time.now.to_f * 10_000_000).to_i end |
#heartbeat=(value) ⇒ Object
371 372 373 |
# File 'lib/pubnub/client.rb', line 371 def heartbeat=(value) @env[:heartbeat] = value 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.
263 264 265 266 267 268 269 270 |
# File 'lib/pubnub/client.rb', line 263 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 |
#region_code ⇒ Object
Retruns:
Current region or default '0'
316 317 318 |
# File 'lib/pubnub/client.rb', line 316 def region_code @env[:region_code] || 0 end |
#region_code=(region) ⇒ Object
Parameters:
- region
- New region.
Returns:
New region.
329 330 331 |
# File 'lib/pubnub/client.rb', line 329 def region_code=(region) @env[:region_code] = region end |
#remove_listener(options) ⇒ Object
172 173 174 |
# File 'lib/pubnub/client.rb', line 172 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.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/pubnub/client.rb', line 234 def request_dispatcher(origin, event_type, sync) Pubnub.logger.debug('Pubnub::Client') do "Looking for requester for #{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
:nocov:
362 363 364 365 |
# File 'lib/pubnub/client.rb', line 362 def sequence_number_for_publish! @env[:sequence_number_for_publish] += 1 @env[:sequence_number_for_publish] % 2**32 end |
#subscribe_filter ⇒ Object
405 406 407 |
# File 'lib/pubnub/client.rb', line 405 def subscribe_filter @env[:subscribe_filter] end |
#subscribe_filter=(filter_expr) ⇒ Object
399 400 401 402 403 |
# File 'lib/pubnub/client.rb', line 399 def subscribe_filter=(filter_expr) @env[:subscribe_filter] = filter_expr @subscriber.reset if subscribed? filter_expr end |
#subscribed? ⇒ Boolean
Returns:
True if client is subscribed to at least one channel or channel group, otherwise false.
187 188 189 190 191 192 193 |
# File 'lib/pubnub/client.rb', line 187 def subscribed? if @subscriber.nil? false else ![@subscriber.channels, @subscriber.groups, @subscriber.wildcard_channels].flatten.empty? end end |
#subscribed_channels ⇒ Object
176 177 178 |
# File 'lib/pubnub/client.rb', line 176 def subscribed_channels @subscriber.channels + @subscriber.wildcard_channels end |
#subscribed_groups ⇒ Object
180 181 182 |
# File 'lib/pubnub/client.rb', line 180 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.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/pubnub/client.rb', line 198 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 |
#timetoken ⇒ Object
Returns:
Current client timetoken
309 310 311 |
# File 'lib/pubnub/client.rb', line 309 def timetoken @env[:timetoken] end |
#timetoken=(timetoken) ⇒ Object
Parameters:
- timetoken
- New timetoken.
Returns:
New timetoken.
342 343 344 |
# File 'lib/pubnub/client.rb', line 342 def timetoken=(timetoken) @env[:timetoken] = timetoken end |
#uuid ⇒ Object
Returns:
Current uuid.
349 350 351 |
# File 'lib/pubnub/client.rb', line 349 def uuid @env[:uuid] end |