Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>PubNub Presence with Ruby and JavaScript</h1> <p>Get started easily with PubNub Presence in Ruby and JavaScript by following this short guide which connects the state between the two PubNub SDKs. First you'll want to make sure you have the latest <strong>PubNub Ruby GEM</strong> client SDK install on your server. But before we get into the coding aspect we can talk about what <strong>PubNub Presence</strong> actually is. </p> <h2>PubNub Presence Introduction</h2> <p>PubNub Presence allows you to ask the question <em>"Who is there?"</em> and receive back an answer in the form of a <strong>List of UserIDs</strong> and an occupancy count of who is currently online right now. </p> <p>Usually you ask this question in the context of a PubNub Channel. User's connect to a PubNub Channel in order to receive a stream of data from the PubNub Network. You control the stream by way of Channels by publishing and subscribing to channels by any valid UTF-8 string. Sometimes you want to know the current state of the PubNub Channel by requesting the current activity and list of connected users on the channel. You can do this by using PubNub Presence feature which provides the answer you seek.</p> <h2>PubNub Presence SDKs</h2> <p>Let's get starte by listing the two starting steps of including/loading the GEM ans JavaScript SDKs for your target platforms (This time it's Ruby+JavaScript Combo).</p> <h3>Install PubNub Ruby GEM</h3> <pre><code>sudo gem install pubnub </code></pre> <p>Next you will ensure that you are running one of the latest JavaScript SDKs on your JavaScript Client App (usually a mobile phone app or website app).</p> <h3>Include PubNub JavaScript Client SDK</h3> <pre><code>&lt;script src=http://cdn.pubnub.com/pubnub-3.4.2.min.js &gt;&lt;/script&gt; </code></pre> <p>Now that you have accessed the two necessary base SDK libs for Ruby and JavaScript, you are able to receive/transmit information freely over the PubNub Network. Next we'll talk about how you can easily receive presence events as they occur on your PubNub Channel and also how you can query directly for the current channel state with <code>here_now()</code> API.</p> <h2>PubNub Dev Console - Presence</h2> <p><img src="https://pubnub.s3.amazonaws.com/assets/dev-console-presence-1.png"></p> <blockquote> <p>Use the <a href="http://www.pubnub.com/console" rel="nofollow noreferrer">PubNub Developer's Console</a> to Monitor Presence Events.</p> </blockquote> <p>You can use the PubNub Developer's Console to watch Presence Events as they occur. You will be able to see the event payload in JSON form in the presence section which is shown in the following image:</p> <p><img src="https://pubnub.s3.amazonaws.com/assets/dev-console-presence-2.png"></p> <p>There are three events <code>"action"</code>s you can receive including:</p> <ul> <li><code>"join"</code> - A new user joined the channel.</li> <li><code>"leave"</code> - A user left the channel.</li> <li><code>"timeout"</code> - A user dropped connection and timed out.</li> </ul> <h2>PubNub Presence with REST</h2> <h3>PubNub Here Now</h3> <p>With PubNub Presence you have access to TWO REST routes. The easiest route is the <code>here_now()</code> route.</p> <h2>PubNub Presence with SDKs</h2> <h3>Example Source Code in JavaScript</h3> <p>The following is an example of method in JavaScript to receive data on a Channel and also receive Presence Events for that channel as they occur in real-time. Also you will notice the paramaters that are passed to you in your Function Callback. This method allows you to receive a stream of data into your JavaScript Application. Note there is a second method to receive presence data (called here_now()) which we will cover a bit further down.</p> <pre><code>&lt;script&gt;(function(){ var pubnub = PUBNUB.init({ subscribe_key : 'demo' }); pubnub.subscribe({ channel : "hello_world", // YOUR CHANNEL. message : function( message, env, channel ) {}, // RECEIVE MESSAGE. presence : function( message, env, channel ) { // PRESENCE EVENTS. console.log( "Channel: ", channel ); console.log( "Join/Leave/Timeout: ", message.action ); console.log( "Occupancy: ", message.occupancy ); console.log( "User ID: ", message.uuid ); } }) })();&lt;/script&gt; </code></pre> <h3>Example Source Code in Ruby</h3> <p>Here is Ruby Code which is the ruby method to receive Presence Events in real-time as they occur. You can process the stream or (Firehose) of events as they come in. Note there is a second method to receive presence data (called here_now()) which we will cover a bit further down.</p> <pre><code>require 'pubnub' pubnub = Pubnub.new( :publish_key =&gt; 'demo', # publish_key only required if publishing. :subscribe_key =&gt; 'demo', # required :secret_key =&gt; nil, # optional, if used, message signing is enabled :cipher_key =&gt; nil, # optional, if used, encryption is enabled :ssl =&gt; nil # true or default is false ) ## Receive Presence Events on a Channel pubnub.presence( :channel =&gt; :hello_world, :callback =&gt; lambda { |event_data| puts(event_data) } ) </code></pre> <p>When an event occurs (such as a user joining) the output data will look like:</p> <pre><code>{"action":"join", "timestamp":1364261400, "uuid":"9d497a30-3af2-4b67-a6b3-82f254711c11", "occupancy":4} </code></pre> <p>on a User Disconnect, the presence event is triggered as:</p> <pre><code>{"action":"leave", "timestamp":1364251540, "uuid":"9d497a30-3af2-4b67-a6b3-82f254711c11", "occupancy":3} </code></pre> <p>and potentially in the event of an error/timeout:</p> <pre><code>{"action":"timeout", "timestamp":1364251540, "uuid":"9d497a30-3af2-4b67-a6b3-82f254711c11", "occupancy":3} </code></pre> <h3>Here_Now in JavaScript</h3> <p>There is a <code>here_now()</code> function available to you that allows you to issue a single REST request to the PubNub Network that gets the <strong>Current</strong> state of a channel's connectivity. The request looks like:</p> <pre><code>&lt;script&gt;(function(){ var pubnub = PUBNUB.init({ subscribe_key : 'demo' }); pubnub.here_now({ channel : 'hello_world', callback : function (message) { console.log(message) } }); })();&lt;/script&gt; </code></pre> <p>and the response object will look like:</p> <pre><code>{"uuids":["754e58b3-a79b-4d91-8f6c-5d994e43a310","175c2c67-b2a9-470d-8f4b-1db94f90e39e","fafd273d-9be5-4049-a6ce-653c467f7c5d"],"occupancy":3} </code></pre> <h3>Here_Now in Ruby</h3> <p>Just like in JavaScript the same function for <code>here_now()</code> is available in Ruby too. Here is the ruby syntax version:</p> <pre><code>require 'pubnub' pubnub = Pubnub.new( :publish_key =&gt; 'demo', # publish_key only required if publishing. :subscribe_key =&gt; 'demo', # required :secret_key =&gt; nil, # optional, if used, message signing is enabled :cipher_key =&gt; nil, # optional, if used, encryption is enabled :ssl =&gt; nil # true or default is false ) pubnub.here_now( :channel =&gt; :hello_world, :callback =&gt; lambda { |event_data| puts(event_data) } ) </code></pre> <p>And the response object data is identical to what is available to you in JavaScript.</p> <pre><code>{"uuids":["754e58b3-a79b-4d91-8f6c-5d994e43a310","175c2c67-b2a9-470d-8f4b-1db94f90e39e","fafd273d-9be5-4049-a6ce-653c467f7c5d"],"occupancy":3} </code></pre> <p>Finally if you want to use the simple JSON REST interface provided by the PubNub Network, you can issue the following requests easily:</p> <pre><code>curl http://pubsub.pubnub.com/v2/presence/sub_key/demo/channel/hello_world </code></pre> <p>and the response output is identical:</p> <pre><code>{"uuids":["754e58b3-a79b-4d91-8f6c-5d994e43a310","175c2c67-b2a9-470d-8f4b-1db94f90e39e","fafd273d-9be5-4049-a6ce-653c467f7c5d"],"occupancy":3} </code></pre> <p>That's it! Super simple and easy to use PubNub Network Presence with Ruby on Rails and JavaScript. If you have any questions please contact PubNub directly and also visit the following links for more details:</p> <ul> <li>PubNub Network Ruby SDK - <a href="https://github.com/pubnub/ruby" rel="nofollow noreferrer">https://github.com/pubnub/ruby</a></li> <li>PubNub Network JavaScript SDK - <a href="https://github.com/pubnub/javascript#simple-example" rel="nofollow noreferrer">https://github.com/pubnub/javascript#simple-example</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload