File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments