Skip to content

Commit 3df9e90

Browse files
committed
Added example with only the chat code.
1 parent 5571ad5 commit 3df9e90

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

chat.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!-- Chat in 10 lines of JavaScript code using PubNub JavaScript V4 SDK (mini version)-->
2+
<p>Enter chat and press enter.</p>
3+
<input id="input" placeholder="Your Message Here"/>
4+
<p>Chat Output:<p>
5+
<div id="box"></div>
6+
7+
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.23.0.min.js"></script>
8+
<script>(function(){
9+
var pubnub = new PubNub({publishKey : 'demo',subscribeKey : 'demo'}); // Your PubNub keys here. Get them from https://dashboard.pubnub.com.
10+
var box = document.getElementById("box"), input = document.getElementById("input"), channel = 'chat';
11+
pubnub.subscribe({channels: [channel]}); // Subscribe to a channel.
12+
pubnub.addListener({message: function(m) {
13+
box.innerHTML = (''+m.message).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML; // Add message to page.
14+
}});
15+
input.addEventListener('keypress', function (e) {
16+
(e.keyCode || e.charCode) === 13 && pubnub.publish({ // Publish new message when enter is pressed.
17+
channel : channel, message : input.value, x : (input.value='')
18+
});
19+
});
20+
})();</script>

0 commit comments

Comments
 (0)