@@ -42,14 +42,14 @@ <H1>Chat in 10 lines of JavaScript. Powered by PubNub</H1>
4242 userId: 'user1'});
4343var channel = '10chat';
4444
45- // Subscribe to a channel and add listener
46- pubnub.subscribe({channels: [channel]});
45+ // Add a listener to a channel and subscribe to it
4746pubnub.addListener({
4847 message: function (m) {
4948 box.innerHTML += newRow(m.message,
5049 m.publisher);
5150 }
5251});
52+ pubnub.subscribe({channels: [channel]});
5353// Publish new message when enter is pressed.
5454input.addEventListener('keypress', function (e) {
5555 (e.keyCode || e.charCode) === 13 &&
@@ -75,13 +75,13 @@ <H1>Chat in 10 lines of JavaScript. Powered by PubNub</H1>
7575 ( function ( ) {
7676 var pubnub = new PubNub ( { publishKey : 'demo' , subscribeKey : 'demo' , userId : id } ) ; // Your PubNub keys here. Get them from https://dashboard.pubnub.com.
7777 var box = document . getElementById ( "outputDiv" ) , input = document . getElementById ( "input" ) , channel = '10chat' ;
78- pubnub . subscribe ( { channels : [ channel ] } ) ; // Subscribe to a channel.
7978 pubnub . addListener ( {
8079 message : function ( m ) {
8180 box . innerHTML += newRow ( m . message , m . publisher ) ;
8281 box . scrollTop = box . scrollHeight ;
8382 }
8483 } ) ;
84+ pubnub . subscribe ( { channels : [ channel ] } ) ; // Subscribe to a channel.
8585 input . addEventListener ( 'keypress' , function ( e ) {
8686 ( e . keyCode || e . charCode ) === 13 && input . value != "" && pubnub . publish ( { // Publish new message when enter is pressed.
8787 channel : channel , message : input . value , x : ( input . value = '' )
0 commit comments