(function(){
var newUUID =PubNub.generateUUID();
var pubnub = new PubNub({uuid: newUUID, publishKey : 'pub-c-07663a0e-b4a5-4bd1-af00-8ba97ee2b9a1', subscribeKey : 'sub-c-432706e2-f830-11e6-b0ac-0619f8945a4f' });
function $(id) { return document.getElementById(id); }
var box = $('box'), input = $('input'), channel = 'room1';
pubnub.addListener({
message: function(obj) {
box.innerHTML = (''+obj.message).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML
},
presence: function(p) {
var action = p.action; // Can be join, leave, state-change or timeout
var channelName = p.channel; // The channel for which the message belongs
var occupancy = p.occupancy; // No. of users connected with the channel
var state = p.state; // User State
var channelGroup = p.subscription; // The channel group or wildcard subscription match (if exists)
var publishTime = p.timestamp; // Publish timetoken
var timetoken = p.timetoken; // Current timetoken
var uuid = p.uuid;
//alert(" user "+uuid +" has "+action )
//alert(occupancy)
if(action === 'state-change') {
if(p.state.isTyping === true) {
alert(uuid + ' is typing...');
}
}
// alert(occupancy)
}
});
pubnub.subscribe({channels:[channel] , withPresence: true });
input.addEventListener('keyup', function(e) {
if ((e.keyCode || e.charCode) === 13) {
if(input.value.length>0)
pubnub.publish({channel : channel,message : input.value,x : (input.value='')});
}
});
pubnub.history(
{
channel: 'room1',
reverse: false, // true to send via post
count: 100, // how many items to fetch
stringifiedTimeToken: false // false is the...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.