Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Created December 19, 2015 01:02
Show Gist options
  • Select an option

  • Save stephenlb/df17eb4ccf136ac869ed to your computer and use it in GitHub Desktop.

Select an option

Save stephenlb/df17eb4ccf136ac869ed to your computer and use it in GitHub Desktop.
GoZXxQ
<!-- Enter Chat and press enter -->
<div>
<input id=input placeholder=chat-here />
<button id=send>send</button>
</div>
<!-- Chat Output -->
<div id=box></div>
(function() {
var box = PUBNUB.$('box'),
send = PUBNUB.$('send'),
input = PUBNUB.$('input'),
channel = 'chat';
PUBNUB = PUBNUB({
publish_key: 'demo',
subscribe_key: 'demo'
})
PUBNUB.subscribe({
channel: channel,
callback: function(text) {
box.innerHTML = ('' + text).replace(/[<>]/g, '') + '<br>' + box.innerHTML
}
});
function deliver() {
PUBNUB.publish({
channel: channel,
message: input.value,
x: (input.value = '')
})
}
PUBNUB.bind('keyup', input, function(e) {
if ((e.keyCode || e.charCode) === 13) deliver();
});
PUBNUB.bind('mousedown,touchstart', send, deliver);
})();
<script src="https://cdn.pubnub.com/pubnub-dev.js"></script>
input,
button,
div {
padding: 10px;
margin: 10px;
font-size: 30px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment