Search code examples
javascriptpubnub

How to Invoke PubNub Chat multiple times on same page?


So I'm trying to fit PubNub's really simple chat code to my needs in having multiple chats on one page. Here's the faulty demo and original code:

  Enter Chat and press enter
<div><input id="input" placeholder="Say something!" /></div> 
Chat Output
<div id="box"></div>


<script src=http://cdn.pubnub.com/pubnub.min.js></script>
<script>
  (function(){
var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'chat';
PUBNUB.subscribe({
channel : channel,
callback : function(text) { box.innerHTML = (''+text).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML }
});
PUBNUB.bind( 'keyup', input, function(e) {
(e.keyCode || e.charCode) === 13 && PUBNUB.publish({
channel : channel, message : input.value, x : (input.value='')
})
} )
})()

</script>

I'm not very good at JS, but I assumed the code in the demo would work, as I just changed the id and variable names. Thanks for the help, if this could work I'd be golden.


Solution

  • There is a missing ; between your two block codes after ()

    // ...
    })
    } )
    })();
    

    ^^ This one ^^

      (function(){
    var box1 = PUBNUB.$('box1'), input1 = PUBNUB.$('input1'), channel = 'chat';
    // ...
    

    Fixed code : http://codepen.io/anon/pen/odhle