Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>Rails 3.0+ Push Content in Real-time with PubNub</h1> <p>In order to safely and securely push real-time updates to your users with some social functionality on your web site or web app there are, as you say, many options available and many nuances to consider with this as well. <strong>There is a gem for this!</strong> And I will help you get started by providing a simple start guide here for your Rails 3.0+ app and web site. First you'll want to run <code>gem install pubnub</code> which is the real-time component known as PubNub that will allow you to send notifications directly to targeted users or a group of users on your website and apps. This allows you to target only those who are online and interested in receiving the notifications from your Rails server. You can consider this as efficient server-sent events that require no DB Lookup or other resource intensive operations on your servers.</p> <h2>Ruby GEM Install</h2> <pre><code>gem install pubnub </code></pre> <h3>Require PubNub GEM</h3> <pre><code>require 'pubnub' </code></pre> <h3>Initialize The PubNub Class</h3> <pre><code>pubnub = Pubnub.new( :publish_key =&gt; 'demo', # publish_key only required if publishing. :subscribe_key =&gt; 'demo', # required always :secret_key =&gt; nil, # optional - message signing :cipher_key =&gt; nil, # optional - AES 256 Crypto :ssl =&gt; true # optional - SSL 2048bit ) </code></pre> <h3>From Rails 3.0+ Send Messages to the Web App</h3> <p>Now from your Rails App with the <code>pubnub</code> class ready, you can send any kind of JSON data type including a String, Number, Array and Dictionary/Object:</p> <ul> <li>a "String"</li> <li>a Number 123</li> <li>an array [ 1, 2, 3 ]</li> <li>an object { :a => "apple" }</li> </ul> <p>The following code will show you the process which will pass a message from your Rails App directly to the browser or web app that you users are running. Essentially sending the data to your users when you need to directly from your server. The <code>pubnub.publish(...)</code> function will serialize the message you send as a JSON for transport to your mobile and web apps.</p> <pre><code>## Broadcast or Unicast to Browser App User(s) pubnub.publish( :channel =&gt; "event_feed_channel", :message =&gt; { :event =&gt; "update", :from =&gt; "Jay", :feed =&gt; "New Updates!" }, :callback =&gt; lambda { |info| puts info } ) </code></pre> <p>This process works great for performance and security considerations and never requires DB queries; saving you from hammering on your Database server and other resource intensive operations. You may consider this process as <strong>SSE (Server-Sent Events)</strong> while you are using the PubNub GEM in your Rails applications. </p> <p>Using the PubNub GEM you are essentially <em>sending events</em> from your Server direct to your User's Browser/App. It removes the need for client-side periodic AJAX requests. This is because you are only sending data to your user's when new information is available. You may also <a href="http://www.pubnub.com" rel="noreferrer">consider using more PubNub Real-time Network features like Presence and Storage/Playback</a> by visiting the home site to learn more details about the Ruby features. And also <a href="http://www.pubnub.com/console" rel="noreferrer">use the Developers Dev Console</a> for debugging if needed during your integration into your Rails code.</p> <h3>Setup Browser to Receive Server-Sent Events</h3> <p>Next you need to setup your app to receive the Server-Sent Events by running init and passing in the </p> <pre><code>&lt;script src=http://cdn.pubnub.com/pubnub-3.4.2.min.js &gt;&lt;/script&gt; &lt;script&gt;(function(){ // Class Setup var pubnub = PUBNUB.init({ subscribe_key : 'demo' }); // Event Hook Setup pubnub.subscribe({ channel : "event_feed_channel", message : function(m){ pubnub.events.fire( m.event, m ) } }); // RECEIVE the "update" Server Broadcast Event in the Browser pubnub.events.bind( "update", function(message) { console.log( "update a &lt;div&gt; with received event: ", m ); } ); })();&lt;/script&gt; </code></pre> <blockquote> <p>Note you may be interested in <a href="https://github.com/pubnub/javascript#advanced-subscribe-connectivity-optionscallbacks" rel="noreferrer">Advanced Subscribe Connectivity Options and Events</a>.</p> </blockquote> <p>Notice how the <code>"update"</code> event is identical to the <code>:event =&gt; "update"</code> message value in the <strong>ruby</strong> code. This means that the event will be triggered based on the name you supply and you need to bind the event to trigger code to execute on your Web App page. Inside this triggered event you will update the User Interface with any relevant details that the user would want to see on their screen in real-time.</p> <p>Also notice the <code>:channel =&gt; "event_feed_channel"</code> is the <code>publish</code> channel, and this must be identical as the <code>subscribe</code> channel in the JavaScript client code.</p> <p>If you want to read the source code of the Ruby GEM, it is available via <a href="https://github.com/pubnub/ruby" rel="noreferrer">GitHub PubNub Ruby-based APIs</a>.</p> <p>These are the basics for getting started with Ruby 3.0+ Apps and sending events directly from your server to your End-users. Note this also works for Mobile Apps that run on iOS Web Browsers or Android Browsers. Also old versions of IE are supported for this mechanism. In addition you could optionally consider server sent events from your Rails servers directly to Native iOS applications and Native Android Applications. If you have any questions ask them here on this Forum.</p>
 

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