Skip to content

Instantly share code, notes, and snippets.

@sebnozzi
Created April 16, 2012 20:14
Show Gist options
  • Save sebnozzi/2401210 to your computer and use it in GitHub Desktop.
Save sebnozzi/2401210 to your computer and use it in GitHub Desktop.
PubNub Chat in 10 lines
<div pub-key="..." sub-key="..." ssl="off" ...></div>
<script src="http://cdn.pubnub.com/pubnub-3.1.min.js"></script>
Enter Chat and press enter
<div><input id="input" value="you-chat-here" /></div>
Chat Output
<div id="output"></div>
<script>
(function(){
var box = PUBNUB.$('output');
var input = PUBNUB.$('input');
var channel = 'chat';
PUBNUB.subscribe({
channel : channel,
callback : function(text) {
box.innerHTML = (''+text).replace( /[<>]/g, '' )
+ '<br/>' + box.innerHTML;
}
});
PUBNUB.bind( 'keyup', input,
function(e) {
(e.keyCode || e.charCode) === 13 && PUBNUB.publish({
channel : channel, message : input.value
});
});
})();</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment