Skip to content

Instantly share code, notes, and snippets.

@mochadwi
Forked from stephenlb/1.html
Last active September 21, 2015 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mochadwi/e159757206c1a93db105 to your computer and use it in GitHub Desktop.
Save mochadwi/e159757206c1a93db105 to your computer and use it in GitHub Desktop.
Enter Chat and press enter
<div><input id=input placeholder=you-chat-here /></div>
Chat Output
<div id=box></div>
<script src=http://cdn.pubnub.com/pubnub.min.js></script>
<script>(function(){
var box = PUBNUB.$('box'), input = PUBNUB.$('input'), 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, x : (input.value='')
})
} )
})()</script>
// Send a message
PUBNUB.publish({ channel : 'chat', message : "hello!" })
// Receive messages
PUBNUB.subscribe({ channel : 'chat', message : fun })
@cvconover
Copy link

Just a heads up...
Change http://cdn.pubnub.com/pubnub.min.js (this points to v3.6.7 - very old)
To: http://cdn.pubnub.com/pubnub-dev.js (this always points to latest release, unminified)
Or use the latest version of the lib if you want to use a minified version.
http://www.pubnub.com/docs/web-javascript/pubnub-javascript-sdk

Cheers
Craig

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment