Skip to content

Instantly share code, notes, and snippets.

@pblca
Created March 26, 2015 19:49
Show Gist options
  • Save pblca/1e8c9f427935726558af to your computer and use it in GitHub Desktop.
Save pblca/1e8c9f427935726558af to your computer and use it in GitHub Desktop.
var pubnub = PUBNUB.init({
publish_key : 'pub-c-df94b72e-7d48-4df1-8876-2cccc1afe0f8',
subscribe_key : 'sub-c-c5b3ec0c-d248-11e4-95a3-0619f8945a4f',
ssl : (('https:' == document.location.protocol) ? true : false)
});
function rand(len){
var text = "";
var charset = "abcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < len; i++ ) {
text += charset.charAt(Math.floor(Math.random() * charset.length));
}
return text;
}
var namespace = rand(10);
var indecies = [
{
symbol: "MDRX",
index : "healthcare"
},{
symbol: "NHC",
index : "healthcare"
},{
symbol: "AAPL",
index : "technology"
},{
symbol: "GOOG",
index : "technology"
},{
symbol: "MSFT",
index : "technology"
},{
symbol: "YHOO",
index : "technology"
},{
symbol: "DTE",
index : "energy"
},{
symbol: "BIOF",
index : "energy"
}
];
function updatePrices(){
for(var i = 0; i<indecies.length; i++){
var sym = indecies[i].symbol;
var ind = indecies[i].index;
var currentPrice = Number($("*[data-priceIndex='"+sym+"'] span.displayedPrice").text());
var random = (Math.floor((Math.random()*25)+1));
var plusOrMinus = Math.random() < 0.5 ? -1 : 1;
random = random * plusOrMinus;
var newPrice = currentPrice + random;
pubnub.publish({
channel: namespace+"-"+ind+"-"+sym,
message: {
symbol : sym,
index : ind,
price : newPrice,
difference : random
},
callback: function(){
}
});
}
}
$(":checkbox").change(function(){
var sym = $(this).data('symbol');
var ind = $(this).data('index');
if(this.checked) {
pubnub.channel_group_add_channel({
callback: function(m){
$("*[data-priceIndex='"+sym+"']").show();
},
error: function(m){
},
channel: namespace+"-"+ind+"-"+sym,
channel_group: namespace+":"+ind
});
} else if (!this.checked){
pubnub.channel_group_remove_channel({
callback: function(m){
$("*[data-priceIndex='"+sym+"']").hide();
},
error: function(m){
},
channels: namespace+"-"+ind+"-"+sym,
channel_group: namespace+":"+ind
});
}
});
function displayNewPrice(message){
var sym = message.symbol;
var ele = $(".priceDisplay[data-priceIndex='"+sym+"']");
$(ele).find(".difference").text(message.difference);
$(ele).find(".displayedPrice").text(message.price);
if(message.difference < 0){
$(ele).children(".diffDisplay").removeClass('positive').removeClass("negative").addClass('negative');
} else {
$(ele).children(".diffDisplay").removeClass('positive').removeClass("negative").addClass('positive');
}
}
pubnub.subscribe({
channel_group: namespace+":technology",
callback: function(m){
displayNewPrice(m);
},
error: function(e){
},
message: function(m){
},
connect: function(){
}
});
pubnub.subscribe({
channel_group: namespace+":healthcare",
callback: function(m){
displayNewPrice(m);
},
error: function(e){
},
message: function(m){
},
connect: function(){
}
});
pubnub.subscribe({
channel_group: namespace+":energy",
callback: function(m){
displayNewPrice(m);
},
error: function(e){
},
message: function(m){
},
connect: function(){
}
});
setInterval(function(){updatePrices()},1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment