Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Forked from domadev812/1.html
Created November 15, 2017 21:05
Show Gist options
  • Select an option

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

Select an option

Save stephenlb/890e240d8dd85c96a80f3055501ff059 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="https://cdn.pubnub.com/sdk/javascript/pubnub.4.17.0.js"></script>
<script>(function(){
var pubnub = new PubNub({
subscribeKey: "demo",
publishKey: "demo",
ssl: true
});
var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'chat';
pubnub.addListener({
status: function(statusEvent) {
},
message: function(message) {
box.innerHTML = (''+text).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML;
},
presence: function(presenceEvent) {
// handle presence
}
})
pubnub.subscribe({
channels: ['channel']
});
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(
{
message: {
such: 'hello!'
},
channel: 'chat',
sendByPost: false, // true to send via post
storeInHistory: false, //override default storage options
meta: {
"cool": "meta"
} // publish extra meta with the request
},
function (status, response) {
// handle status, response
}
);
pubnub.addListener({
status: function(statusEvent) {
},
message: function(message) {
box.innerHTML = (''+text).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML;
},
presence: function(presenceEvent) {
// handle presence
}
})
pubnub.subscribe({
channels: ['channel']
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment