Last active
March 18, 2016 01:29
-
-
Save scalabl3/0735f06495d09cf5f764 to your computer and use it in GitHub Desktop.
Getting Started Tutorial for Using PubNub Presence with JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Include the PubNub Library --> | |
<script src="https://cdn.pubnub.com/pubnub.min.js"></script> | |
<!-- Instantiate PubNub --> | |
<script type="text/javascript"> | |
var PUBNUB_demo = PUBNUB.init({ | |
publish_key: 'demo', | |
subscribe_key: 'demo' | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Subscribe to the demo_tutorial channel with presence and state | |
PUBNUB_demo.subscribe({ | |
channel: 'demo_tutorial', | |
noheresync: true, // don't do a here_now call on subscribe() | |
message: function(m){ | |
console.log(m); | |
}, | |
presence: function(m) { | |
console.log(m); | |
}, | |
state: { | |
name: 'presence-tutorial-user', | |
timestamp: new Date() | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Unsubcribe to the channel, also unsubscribes to presence event channel | |
PUBNUB_demo.unsubscribe({ | |
channel: 'demo_tutorial' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//See who the uuid's & state objects of those currently subscribed | |
PUBNUB_demo.here_now({ | |
channel: 'demo_tutorial', | |
state: true, // return the state object for each also | |
callback: function(msg) { | |
console.log(msg); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Getting Started with PubNub Presence</title> | |
</head> | |
<body> | |
<script src="https://cdn.pubnub.com/pubnub.min.js"></script> | |
<script type="text/javascript" charset="utf-8"> | |
var PUBNUB_demo = PUBNUB.init({ | |
publish_key: 'demo', | |
subscribe_key: 'demo' | |
}); | |
PUBNUB_demo.subscribe({ | |
channel: 'demo_tutorial', | |
message: function(msg){ | |
console.log("message: ", msg); | |
}, | |
presence: function(msg) { | |
console.log("presence: ", msg); | |
}, | |
noheresync: true, | |
state: { | |
name: "User Name", | |
email: "[email protected]", | |
timestamp: new Date() | |
}, | |
connect: function() { | |
PUBNUB.here_now({ | |
channel: 'demo_tutorial', | |
state: true, | |
callback: function(msg) { | |
console.log("here_now(): ", msg); | |
} | |
}); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment