Copy and Paste this code to the bottom of your <BODY> tag.
This gives you access to PUBNUB.publish() and PUBNUB.subscribe() functions and more!
<div id=pubnub pub-key=demo sub-key=demo></div>
<script src="http://cdn.pubnub.com/pubnub-3.4.min.js"></script>
<script>(function(){
PUBNUB.subscribe({
channel : "my_channel",
message : function(m){ alert(m) },
connect : function(channel) {
PUBNUB.publish({ channel : "my_channel", message : "Hi" })
}
})
})();</script>
More great examples with JavaScript are available here on PubNub's JavaScript GitHub Repository.
<div id=pubnub></div>
<script src="http://cdn.pubnub.com/pubnub-3.4.min.js"></script>
<script>(function(){
// INIT PubNub
var pubnub = PUBNUB.init({
publish_key : 'demo',
subscribe_key : 'demo'
});
// LISTEN
pubnub.subscribe({
channel : "my_channel",
message : function(m){ alert(m) },
connect : function(channel) {
// SEND
pubnub.publish({ channel : "my_channel", message : "Hi." })
}
})
})();</script>
<div id=pubnub pub-key=demo sub-key=demo></div>
<script src="http://cdn.pubnub.com/pubnub-3.4.min.js"></script>
<script>(function(){
// LISTEN FOR MESSAGES
PUBNUB.subscribe({
channel : "my_channel", // CONNECT TO THIS CHANNEL.
restore : false, // STAY CONNECTED, EVEN WHEN BROWSER IS CLOSED
// OR WHEN PAGE CHANGES.
callback : function(message) { // RECEIVED A MESSAGE.
alert(message)
},
connect : function() { // CONNECTION ESTABLISHED.
PUBNUB.publish({ // SEND A MESSAGE.
channel : "my_channel",
message : "Hi from PubNub."
})
},
disconnect : function() { // LOST CONNECTION.
alert(
"Connection Lost." +
"Will auto-reconnect when Online."
)
},
reconnect : function() { // CONNECTION RESTORED.
alert("And we're Back!")
},
presence : function(message) {
console.log(message, true);
}
})
})();</script>
*** This JavaScript include file is unique to your account. It includes keys which link to your account.
*** PubNub JavaScript Push API does not depend on other libraries.
*** Do not download and host this file yourself. Otherwise updates will not reach your website.
JavaScript Push API Reference