Class: Pubnub::Client

Inherits:
Object show all
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

VERSION =
Pubnub::VERSION

Constants included from Events

Events::EVENTS

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fire

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.message },
  connect_callback: ->(message) { puts message },
  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(options)
  env_hash = symbolize_options_keys(options)
  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

#envObject (readonly)

Returns the value of attribute env.



96
97
98
# File 'lib/pubnub/client.rb', line 96

def env
  @env
end

#subscriberObject (readonly)

Returns the value of attribute subscriber.



96
97
98
# File 'lib/pubnub/client.rb', line 96

def subscriber
  @subscriber
end

#telemetryObject (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(options)
  @subscriber.add_listener(options)
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_modulePubnub::Crypto::CryptoProvider?

Data processing crypto module.

Returns:



350
351
352
# File 'lib/pubnub/client.rb', line 350

def crypto_module
  @env[:crypto_module]
end

#empty_state?Boolean

Returns:

  • (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_orttObject



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(options)
  @subscriber.remove_listener(options)
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.

Returns:

  • (Boolean)


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_channelsObject



204
205
206
# File 'lib/pubnub/client.rb', line 204

def subscribed_channels
  @subscriber.channels + @subscriber.wildcard_channels
end

#subscribed_groupsObject



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