Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@scalabl3
Last active March 18, 2016 01:29
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save scalabl3/0735f06495d09cf5f764 to your computer and use it in GitHub Desktop.
Save scalabl3/0735f06495d09cf5f764 to your computer and use it in GitHub Desktop.
Getting Started Tutorial for Using PubNub Presence with JavaScript
<!-- 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>
//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()
}
});
//Unsubcribe to the channel, also unsubscribes to presence event channel
PUBNUB_demo.unsubscribe({
channel: 'demo_tutorial'
});
//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);
}
});
<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: "users.email@domain.com",
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