### Full Pubnub Client Example with Listeners and Subscribe (Ruby) Source: https://github.com/pubnub/ruby/blob/master/docs.md A comprehensive example demonstrating the initialization of a Pubnub client, the creation and addition of multiple listener callback objects with names, and subscribing to a channel and its presence channel. Includes example output showing received events. ```ruby # Init pubnub client pubnub_client = Pubnub.new(subscribe_key: 'demo', publish_key: 'demo') # First callbacks object callbacks0 = Pubnub::SubscribeCallback.new( message: ->(envelope) { puts "C0 MESSAGE: #{envelope.result[:data][:message]}" }, presence: ->(envelope) { puts "C0 PRESENCE: #{envelope.result[:data][:message]}" }, status: ->(envelope) { puts "C0 STATUS: #{envelope.result[:data][:message]}" } ) # Second callbacks object callbacks1 = Pubnub::SubscribeCallback.new( message: ->(envelope) { puts "C1 MESSAGE: #{envelope.result[:data][:message]}" }, presence: ->(envelope) { puts "C1 PRESENCE: #{envelope.result[:data][:message]}" }, status: ->(envelope) { puts "C1 STATUS: #{envelope.result[:data][:message]}" } ) # Adding listener allows you to specify name, it's not required to specify a name pubnub_client.add_listener(name: 'c0', callback: callbacks0) # Let's subscribe somewhere pubnub_client.subscribe(channel: :demo, presence: :demo) # SOME OUTPUT: # C0 PRESENCE: {\"action\"=>\"join\", \"timestamp\"=>1461683357, \"uuid\"=>\"fc0c0460-44b4-4338-b7e9-1b534b85072e\", \"occupancy\"=>2} # C0 MESSAGE: {\"text\"=>\"hey\"} ``` -------------------------------- ### Installing PubNub Ruby Gem (Command Line) Source: https://github.com/pubnub/ruby/blob/master/docs.md Provides the command line instruction to install the PubNub Ruby gem using the standard `gem` command. This is the typical way to install the gem globally or within a specific Ruby environment. ```Ruby gem install pubnub ``` -------------------------------- ### Install PubNub Ruby Gem Source: https://github.com/pubnub/ruby/blob/master/README.md Provides the command-line instruction to install the PubNub Ruby SDK using the RubyGems package manager, which is the standard way to add Ruby libraries to a project. ```Bash gem install pubnub ``` -------------------------------- ### Example PubNub Operation Response Structure - Ruby Source: https://github.com/pubnub/ruby/blob/master/Pubnub Ruby SDK upgrade guide.md This snippet shows the structure of a PubNub response object in Ruby, typically obtained after performing an operation like fetching history. It includes details about the HTTP request and response (headers, status code, URI) and the data returned by the PubNub service. ```Ruby @header_item= [["Date", "Wed, 16 Nov 2016 15:36:34 GMT"], ["Content-Type", "text/javascript; charset=\"UTF-8\""], ["Content-Length", "69"], ["Connection", "keep-alive"], ["Cache-Control", "no-cache"], ["Access-Control-Allow-Origin", "*"], ["Access-Control-Allow-Methods", "GET"], ["Accept-Ranges", "bytes"], ["Age", "0"], ["Server", "Pubnub"]], @http_version="1.1", @is_request=false, @reason_phrase="OK", @request_absolute_uri=nil, @request_method="GET", @request_query=nil, @request_uri= #, @status_code=200>, @peer_cert=nil, @previous=nil>, :data=>{:messages=>[90, 91, 92, 93, 94, 95, 96, 97, 98, 99], :end=>14793103423331645, :start=>14793103373028625}}, @status= {:code=>200, :client_request=> #, :server_response=> #, @http_header= #, @body_size=0, @body_type=nil, @chunked=false, @dumped=false, @header_item= [["Date", "Wed, 16 Nov 2016 15:36:34 GMT"], ["Content-Type", "text/javascript; charset=\"UTF-8\""], ["Content-Length", "69"], ["Connection", "keep-alive"], ["Cache-Control", "no-cache"], ["Access-Control-Allow-Origin", "*"], ["Access-Control-Allow-Methods", "GET"], ["Accept-Ranges", "bytes"], ["Age", "0"], ["Server", "Pubnub"]], @http_version="1.1", @is_request=false, @reason_phrase="OK", @request_absolute_uri=nil, @request_method="GET", @request_query=nil, @request_uri= #, @status_code=200>, @peer_cert=nil, @previous=nil>, :category=>:ack, :error=>false, :auto_retried=>false, :data=>nil, :current_timetoken=>nil, :last_timetoken=>nil, :subscribed_channels=>nil, :subscribed_channel_groups=>nil, :config=>{:tls=>false, :uuid=>"96ba2f54-955c-4fd3-acb7-a8407dae2269", :auth_key=>nil, :origin=>"pubsub.pubnub.com"}}, @timetoken=nil> ``` -------------------------------- ### Subscribing to a Channel in Ruby Source: https://github.com/pubnub/ruby/blob/master/docs.md Basic example of subscribing to a single channel ('my_channel') using the PubNub Ruby SDK. This initiates the subscription process, and messages/presence events for this channel will be delivered to registered listeners. Requires a PubNub client instance (`pubnub`). ```Ruby # Subscribe to channel 'my_channel'. pubnub.subscribe( channel: :my_channel ) ``` -------------------------------- ### Fetching Where Now Information in Ruby Source: https://github.com/pubnub/ruby/blob/master/docs.md Provides an example of using the `where_now` method to retrieve the list of channels a specific UUID is subscribed to and processing the result. ```ruby pubnub.where_now( uuid: "my_uuid" ) do |envelope| puts envelope.result[:data] end ``` -------------------------------- ### Upgrading Pubnub Subscribe Code (Ruby) Source: https://github.com/pubnub/ruby/blob/master/Pubnub Ruby SDK upgrade guide.md Compares the old Pubnub SDK v3.8 subscribe syntax with the new v4 approach, demonstrating the creation and attachment of a listener before calling subscribe. ```Ruby # Old one pubnub.subscribe(channel: :demo){ |envelope| puts envelope.message } # New one listener = Pubnub::SubscribeCallback.new( message: ->(envelope){ puts envelope.result[:data] }, status: ->(_e){}, presence: ->(_e){} ) pubnub.add_listener(callback: listener) pubnub.subscribe(channel: :demo) ``` -------------------------------- ### Subscribing to Channel Group in Ruby Source: https://github.com/pubnub/ruby/blob/master/docs.md Provides an example of subscribing to a specific channel group using the `subscribe` method. ```ruby pubnub.subscribe( channel_group: 'ruby_group' ) ``` -------------------------------- ### Creating a Pubnub Subscribe Listener (Ruby) Source: https://github.com/pubnub/ruby/blob/master/Pubnub Ruby SDK upgrade guide.md Demonstrates how to create a Pubnub::SubscribeCallback listener instance with callbacks for message, presence, and status events. This is the new way to handle subscribe events in SDK v4. ```Ruby listener = Pubnub::SubscribeCallback.new( # Callback for messages published on subscribed channels message: lambda do |envelope| puts "MESSAGE: #{envelope.result[:data]}" end, # Callback for presence events from subscribed channels presence: lambda do |envelope| puts "PRESENCE: #{envelope.result[:data]}" end, # Status callbacks status: lambda do |envelope| puts "\n\n\n#{envelope.status}\n\n\n" if envelope.error? # Something bad happend puts "ERROR! #{envelope.status[:category]}" else # Connected! puts 'Connected!' if envelope.status[:last_timetoken] == 0 end end ) ``` -------------------------------- ### Pubnub Ruby SetState Envelope Object Inspection Source: https://github.com/pubnub/ruby/blob/master/Pubnub Ruby SDK upgrade guide.md This snippet shows the detailed structure of the Pubnub::Envelope object returned after a SetState operation. It includes event details, options, result (which is nil in this example), status information (including code, operation, client request URI, server response, data, category, and error flag), and timetoken. The `status[:error]` field is highlighted as a key indicator of success or failure. ```Ruby #:demo, :state=>{:one=>1}, :http_sync=>true, :callback=>nil}, @id="600bd479-8749-4278-8276-43cc7deca12a", @result=nil, @status= {:code=>200, :operation=>:set_state, :client_request=> #, :server_response=> #, @http_header= #, @body_size=0, @body_type=nil, @chunked=false, @dumped=false, @header_item= [["Date", "Wed, 16 Nov 2016 15:43:10 GMT"], ["Content-Type", "text/javascript; charset=\"UTF-8\""], ["Content-Length", "78"], ["Connection", "keep-alive"], ["Access-Control-Allow-Origin", "*"], ["Access-Control-Allow-Methods", "OPTIONS, GET, POST"], ["cache-control", "no-cache"], ["Accept-Ranges", "bytes"], ["Age", "0"], ["Server", "Pubnub Presence"]], @http_version="1.1", @is_request=false, @reason_phrase="OK", @request_absolute_uri=nil, @request_method="GET", @request_query=nil, @request_uri= #, @status_code=200>, @peer_cert=nil, @previous=nil>, :data=>nil, :category=>:ack, :error=>false, :auto_retried=>false, :current_timetoken=>nil, :last_timetoken=>nil, :subscribed_channels=>nil, :subscribed_channel_groups=>nil, :config=>{:tls=>false, :uuid=>"96ba2f54-955c-4fd3-acb7-a8407dae2269", :auth_key=>nil, :origin=>"pubsub.pubnub.com"}}, @timetoken=nil> ``` -------------------------------- ### Getting Channel Occupancy Method Signature (Ruby) Source: https://github.com/pubnub/ruby/blob/master/docs.md Describes the method signature for retrieving current channel occupancy and user lists using the PubNub Ruby SDK. It includes parameters for specifying channels or groups to get occupancy results. ```Ruby #here_now(channel: channel) ``` -------------------------------- ### Publishing Message to Channel in Ruby Source: https://github.com/pubnub/ruby/blob/master/docs.md Basic example of publishing a message to a specific channel ('my_channel') using the PubNub Ruby SDK. It includes a block to handle the publish response envelope and print the status. Requires a PubNub client instance (`pubnub`). ```Ruby pubnub.publish( channel: 'my_channel', message: { text: 'Hi!' } ) do |envelope| puts envelope.status end ``` -------------------------------- ### Get State - Ruby Source: https://github.com/pubnub/ruby/blob/master/docs.md The state API is used to get key/value pairs specific to a subscriber uuid. State information is supplied as a JSON object of key/value pairs. The example shows how to retrieve state for a specific channel and uuid. ```Ruby pubnbu.state(channel: :my_channel, uuid: 'some-uuid') do |envelope| puts envelope.result[:data][:state] end ``` -------------------------------- ### Attaching a Listener to Pubnub Client (Ruby) Source: https://github.com/pubnub/ruby/blob/master/Pubnub Ruby SDK upgrade guide.md Shows how to attach a previously created Pubnub::SubscribeCallback listener instance to the Pubnub client object to receive subscribe events. ```Ruby pubnub.add_listener(callback: listener) ``` -------------------------------- ### Pubnub WhereNow Result Object Structure - Ruby Source: https://github.com/pubnub/ruby/blob/master/Pubnub Ruby SDK upgrade guide.md This snippet shows the structure of the Pubnub::Envelope object returned by a successful `where_now` call in the Ruby SDK. The most important part, containing the list of channels the UUID is on, is accessible via `result[:data]`. ```Ruby #"96ba2f54-955c-4fd3-acb7-a8407dae2269", :http_sync=>true, :callback=>nil}, @id="47066b73-40b2-4ed6-a314-8dd4e0ed9e80", @result= {:code=>200, :operation=>:where_now, :client_request= #, :server_response= #, @http_header= #, @body_size=0, @body_type=nil, @chunked=false, @dumped=false, @header_item= [["Date", "Wed, 16 Nov 2016 15:38:09 GMT"], ["Content-Type", "text/javascript; charset=\"UTF-8\""], ["Content-Length", "90"], ["Connection", "keep-alive"], ["Access-Control-Allow-Origin", "*"], ["Access-Control-Allow-Methods", "OPTIONS, GET, POST"], ["cache-control", "no-cache"], ["Accept-Ranges", "bytes"], ["Age", "0"], ["Server", "Pubnub Presence"]], @http_version="1.1", @is_request=false, @reason_phrase="OK", @request_absolute_uri=nil, @request_method="GET", @request_query=nil, @request_uri= #, @status_code=200>, @peer_cert=nil, @previous=nil>, :data=>{"channels"=>["demo"]}}, @status= {:code=>200, :client_request= #, :server_response= #, @http_header= #, @body_size=0, @body_type=nil, @chunked=false, @dumped=false, @header_item= [["Date", "Wed, 16 Nov 2016 15:38:09 GMT"], ["Content-Type", "text/javascript; charset=\"UTF-8\""], ["Content-Length", "90"], ["Connection", "keep-alive"], ["Access-Control-Allow-Origin", "*"], ["Access-Control-Allow-Methods", "OPTIONS, GET, POST"], ["cache-control", "no-cache"], ["Accept-Ranges", "bytes"], ["Age", "0"], ["Server", "Pubnub Presence"]], @http_version="1.1", @is_request=false, @reason_phrase="OK", @request_absolute_uri=nil, @request_method="GET", @request_query=nil, @request_uri= #, @status_code=200>, @peer_cert=nil, @previous=nil>, :data=>nil, :category=>:ack, :error=>false, :auto_retried=>false, :current_timetoken=>nil, :last_timetoken=>nil, :subscribed_channels=>nil, :subscribed_channel_groups=>nil, :config=>{:tls=>false, :uuid=>"96ba2f54-955c-4fd3-acb7-a8407dae2269", :auth_key=>nil, :origin=>"pubsub.pubnub.com"}}, @timetoken=nil> ``` -------------------------------- ### PubNub Publish Operation Result Structure - Ruby Source: https://github.com/pubnub/ruby/blob/master/Pubnub Ruby SDK upgrade guide.md Details the structure of the Pubnub::Envelope object returned after a publish operation, highlighting the status, event options, and server response details. Notes the importance of the `status[:error]` field for checking for errors. ```Ruby #:demo, :message=>"hello!", :http_sync=>true, :callback=>nil}, @id="c1443a70-5ee5-4e5d-9498-d3d1442c4c20", @result=nil, @status= {:code=>200, :operation=>:publish, :client_request= #, :server_response= #, @http_header= #, @body_size=0, @body_type=nil, @chunked=false, @dumped=false, @header_item= [["Date", "Wed, 16 Nov 2016 15:41:40 GMT"], ["Content-Type", "text/javascript; charset=\"UTF-8\""], ["Content-Length", "30"], ["Connection", "keep-alive"], ["Cache-Control", "no-cache"], ["Access-Control-Allow-Origin", "*"], ["Access-Control-Allow-Methods", "GET"]], @http_version="1.1", @is_request=false, @reason_phrase="OK", @request_absolute_uri=nil, @request_method="GET", @request_query=nil, @request_uri= #, @status_code=200>, @peer_cert=nil, @previous=nil>, :data=>nil, :category=>:ack, :error=>false, :auto_retried=>false, :current_timetoken=>nil, :last_timetoken=>nil, :subscribed_channels=>nil, :subscribed_channel_groups=>nil, :config=>{:tls=>false, :uuid=>"96ba2f54-955c-4fd3-acb7-a8407dae2269", :auth_key=>nil, :origin=>"pubsub.pubnub.com"}}, @timetoken=nil> ``` -------------------------------- ### Retrieve Message History using Pubnub Ruby Source: https://github.com/pubnub/ruby/blob/master/Pubnub Ruby SDK upgrade guide.md This snippet demonstrates calling the `history` method on a Pubnub client instance (`p`) to fetch the last 10 messages from the 'demo' channel synchronously. The `channel` parameter specifies the target channel, `http_sync` ensures a blocking call, and `count` limits the number of messages returned. ```ruby p.history(channel: :demo, http_sync: true, count: 10) ``` -------------------------------- ### PubNub Leave Operation Result Structure - Ruby Source: https://github.com/pubnub/ruby/blob/master/Pubnub Ruby SDK upgrade guide.md Details the structure of the Pubnub::Envelope object returned after a leave operation, highlighting the status, event options, and server response details. Notes the importance of the `status[:error]` field for checking for errors. ```Ruby #:demo, :http_sync=>true, :callback=>nil}, @id="898e2c77-be07-4810-bd41-3ac3632331d8", @result=nil, @status= {:code=>200, :operation=>:leave, :client_request= #, :server_response= #, @http_header= #, @body_size=0, @body_type=nil, @chunked=false, @dumped=false, @header_item= [["Date", "Wed, 16 Nov 2016 15:40:04 GMT"], ["Content-Type", "text/javascript; charset=\"UTF-8\""], ["Content-Length", "74"], ["Connection", "keep-alive"], ["Access-Control-Allow-Origin", "*"], ["Access-Control-Allow-Methods", "OPTIONS, GET, POST"], ["cache-control", "no-cache"], ["Accept-Ranges", "bytes"], ["Age", "0"], ["Server", "Pubnub Presence"]], @http_version="1.1", @is_request=false, @reason_phrase="OK", @request_absolute_uri=nil, @request_method="GET", @request_query=nil, @request_uri= #, @status_code=200>, @peer_cert=nil, @previous=nil>, :data=>nil, :category=>:ack, :error=>false, :auto_retried=>false, :current_timetoken=>nil, :last_timetoken=>nil, :subscribed_channels=>nil, :subscribed_channel_groups=>nil, :config=>{:tls=>false, :uuid=>"96ba2f54-955c-4fd3-acb7-a8407dae2269", :auth_key=>nil, :origin=>"pubsub.pubnub.com"}}, @timetoken=nil> ``` -------------------------------- ### Examining PubNub State Envelope in Ruby Source: https://github.com/pubnub/ruby/blob/master/Pubnub Ruby SDK upgrade guide.md This snippet displays the structure of a Pubnub::Envelope object received after a state operation. It shows the various attributes like event, options, result, and status, indicating that the relevant state data is found within the `result[:data]` hash. ```Ruby #:demo, :http_sync=>true, :uuid=>"96ba2f54-955c-4fd3-acb7-a8407dae2269", :callback=>nil}, @id="156996a8-1a9c-40d2-a6e5-239ddc583bef", @result= {:code=>200, :operation=>:get_state, :client_request= #, :server_response= #, @http_header= #, @body_size=0, @body_type=nil, @chunked=false, @dumped=false, @header_item= [["Date", "Wed, 16 Nov 2016 15:44:24 GMT"], ["Content-Type", "text/javascript; charset=\"UTF-8\""], ["Content-Length", "145"], ["Connection", "keep-alive"], ["Access-Control-Allow-Origin", "*"], ["Access-Control-Allow-Methods", "OPTIONS, GET, POST"], ["cache-control", "no-cache"], ["Accept-Ranges", "bytes"], ["Age", "0"], ["Server", "Pubnub Presence"]], @http_version="1.1", @is_request=false, @reason_phrase="OK", @request_absolute_uri=nil, @request_method="GET", @request_query=nil, @request_uri= #, @status_code=200>, @peer_cert=nil, @previous=nil>, :data=>{:state=>{"one"=>1}, :channel=>"demo"}}, @status= {:code=>200, :client_request= #, :server_response= #, @http_header= #, @body_size=0, @body_type=nil, @chunked=false, @dumped=false, @header_item= [["Date", "Wed, 16 Nov 2016 15:44:24 GMT"], ["Content-Type", "text/javascript; charset=\"UTF-8\""], ["Content-Length", "145"], ["Connection", "keep-alive"], ["Access-Control-Allow-Origin", "*"], ["Access-Control-Allow-Methods", "OPTIONS, GET, POST"], ["cache-control", "no-cache"], ["Accept-Ranges", "bytes"], ["Age", "0"], ["Server", "Pubnub Presence"]], @http_version="1.1", @is_request=false, @reason_phrase="OK", @request_absolute_uri=nil, @request_method="GET", @request_query=nil, @request_uri= #, @status_code=200>, @peer_cert=nil, @previous=nil>, :data=>nil, :category=>:ack, :error=>false, :auto_retried=>false, :current_timetoken=>nil, :last_timetoken=>nil, :subscribed_channels=>nil, :subscribed_channel_groups=>nil, :config=>{:tls=>false, :uuid=>"96ba2f54-955c-4fd3-acb7-a8407dae2269", :auth_key=>nil, :origin=>"pubsub.pubnub.com"}}, @timetoken=nil> ```