The community forums are in BETA and closed to the public.

Javascript syntax with pubnub keys simple chat application

Hi, I have a very simple chat application I modified slightly from a pubnub demo until I try to implement my own publish and subscribe keys. I feel pretty sure that this is a simple syntax issue but I'm very new to javascript and pubnub. I would be very appreciative if someone could help me here. The code is below (and very brief):

Enter Chat and press enter
<div><input id=input placeholder=you-chat-here /></div>

Chat Output
<div id=box></div>

<script src=></script>
  var PUBNUB = PUBNUB({ subscribe_key : 'my subscribe key', publish_key : 'my publish key' }); 
  // if I take the above line out it works fine.  However, when I use this with a Corona app, it gets a quota 
 //  error because the app sends too much data
  // so I need to use my private publish and subscribe keys to avoid that.  Somehow this code uses the  
  //"demo" keys even though its not stated explicitly?
  var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'z';
  channel : channel,
  callback : function(text) {
  var tst = JSON.stringify(text);
  var obj = eval ("(" + (''+tst).replace( /[<>]/g, '' ) + ")");
  box.innerHTML = obj['msgtext'] + '<br>' + box.innerHTML; }
  PUBNUB.bind( 'keyup', input, function(e) {
  (e.keyCode || e.charCode) === 13 &&
  channel : channel,
  message : { "msgtext": input.value },
  x : (input.value='')

Hey Guy,

Glad to hear you're getting started with PubNub. I fixed your code above and hosted it in a CodePen here:

You were correct in debugging this line: var PUBNUB = PUBNUB({ subscribe_key : 'my subscribe key', publish_key : 'my publish key' });

There are two problems with this. The first is that to initialize PubNub in JS you use PUBNUB.init().

var pubnub = PUBNUB.init({                                  
    publish_key   : 'demo',
    subscribe_key : 'demo'

The second is that assigning the result of PUBNUB to a variable of the same name overwrites the global variable and it's methods. This will totally break stuff because now PUBNUB is equal to the result of the init function rather than containing our entire library. Typically in Javascript (and other languages) variables that are in all caps are considered global ain't nothin to mess with, just like the Wu Tang Clan.

Try using lowercase pubnub instead or even connection or socket.

Ian, thanks a million! That worked great. So now I'm sending an array which contain a phone number, time in 1/4 seconds and the x position on the mobile device every time the app users touches or moves their finger. I can see in dev/console that the array is coming through via pubnub with my private keys. Now, what I'm trying to figure out is the smartest way to take that array data and write it to some kind of database, like MySQL or MongoDB. By the way, for beginner seeking to get a handle on javascript syntax, I found the book called "A Smarter Way to Learn Javascript" is an excellent choice as it has lots of online interactive exercises. Here is a link to their site:

I posted another thread on the subject I'm working on complete with the all the Corona SDK code.

Comments to this discussion are now closed!