3. What you will Do/Learn Today
● Publish data w/ PubNub JS SDK
● JavaScript
● Realtime Data Visualization
● Twitter widget
● Facebook API + local servers
@lizziepika
4. What is PubNub?
● Realtime Global Data Stream Network
(DSN)
● Provides back-end for many apps
● SAAS
● Supports > 70 SDKs
@lizziepika
5. What can be streamed?
● Data
● no images or other media
● On other hand….
● YouTube = continuous streaming
media
● No downloading@lizziepika
7. Data Delivery Types
● Content Delivery Network (CDN)
● Data Stream Network (DSN)
● Both send data based on location
● CDN = static, DSN = realtime data
@lizziepika
8. Publish/Subscribe
● Senders characterize messages into classes
● Don’t know receiver
● Indirect receivers = subscribers
● Subscribers choose
● Common in software architecture
@lizziepika
17. Setup
● Git clone repo || download zip
● Incomplete + full versions
● Some HTML, CSS done for you
● https://github.com/pubnub/javascript-realtime-
voting-app-workshop
@lizziepika
18. JS SDK
●Install from CDN
● <script src=”http//cdn.pubnub.com/pubnub-3.x.x.js">
</script>
● At the moment, 3.4.4
● OR Install via Node.JS
● $ npm install pubnub
@lizziepika
19. Required Variables
● var pub_key = "your-pub-key";
● var sub_key = "your-sub-key";
● var chan = "Spectra";
@lizziepika
20. Poll Options + Votes
● Dictionary:
● key = name
● value = count
var pollOptions = {
eon: {
"Mushu": 0, "Stephen": 0,"Tomomi": 0,"Erlich":0, “Ian”:0
}
};
@lizziepika
21. Create instance of object
● init() creates instance of PubNub object
● Need this to invoke PubNub operations,
methods
var pb = PUBNUB.init({
publish_key: pub_key,
subscribe_key: sub_key
});
@lizziepika
22. HTML buttons via JS
● In function setupButtons()
● Create array of buttons
function setupButtons() {
for(key in pollOptions.eon) {
var b = document.createElement('BUTTON');
b.setAttribute('id', 'button' + key);
b.setAttribute('width', '30%');
b.innerHTML = key;
@lizziepika
32. Generate your own API keys
● http://www.pubnub.com
● ->Get started
● ->Enter new app name
● -> create new app
● Get keys
● ->Enable Storage and Playback
●(history)
@lizziepika
39. Get Facebook app Keys
● Go to My apps then Add a new app
● Create new app, get keys
● Uncomment in your opening HTML tag:
...lang="pt"
xmlns:fb="http://www.facebook.com/2008/fbml">
@lizziepika
40. init. Facebook API
After drawChart() in JS file...
● window.fbAsyncInit = function() {
● FB.init({
● appId : 'your-app-id',
● xfbml : true,
● version : 'v2.7'
● });
● };
@lizziepika
41. Share + Like buttons
Uncomment, but find the difference
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
@lizziepika
42. Auto post status
function myFacebookLogin() {
FB.login(function(){
FB.api('/me/feed', 'post', {message: 'Hello, world!
Having fun building a realtime voting app with
#PubNub and #Twitter and #Facebook APIs
#sospectra'});
}, {scope: 'publish_actions'});
}
@lizziepika
43. FB API in HTML file
<button onclick="myFacebookLogin()">Post prev-composed
status</button>
<div
class="fb-like"
data-share="true"
data-width="450"
data-show-faces="true">
</div>
@lizziepika