-
-
Save joshmarinacci/246c64e009fc8202d0fb7442fc48d22c to your computer and use it in GitHub Desktop.
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
Enter Chat and press enter | |
<div><input id=input placeholder=you-chat-here /></div> | |
Chat Output | |
<div id=box></div> | |
<script src=https://cdn.pubnub.com/sdk/javascript/pubnub.4.0.11.min.js></script> | |
<script>(function(){ | |
var pubnub = new PubNub({ publishKey : 'demo', subscribeKey : 'demo' }); | |
function $(id) { return document.getElementById(id); } | |
var box = $('box'), input = $('input'), channel = '10chat-demo'; | |
pubnub.addListener({ | |
message: function(obj) { | |
box.innerHTML = (''+obj.message).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML | |
}}); | |
pubnub.subscribe({channels:[channel]}); | |
input.addEventListener('keyup', function(e) { | |
if ((e.keyCode || e.charCode) === 13) { | |
pubnub.publish({channel : channel,message : input.value,x : (input.value='')}); | |
} | |
}); | |
})();</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
// Send a message | |
pubnub.publish({ channel : 'chat', message : "hello!" }) | |
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
pubnub.addListener({ | |
message: function(msg) { console.log("got a message",msg.message); } | |
}); | |
pubnub.subscribe({channels:['chat']}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hola