Plotting realtime values using PubNub

I’m trying to use the PubNub class to stream data to a chart like this: http://www.pubnub.com/developers/eon/chart/spline/
The imp PubNub class description says it takes message data in any basic type (string, integer, etc), an array, or an object. But looking at their “dummy publish” example…

pubnub.publish({ channel: channel, message: { columns: [ ['x', new Date().getTime()], ['Austin', Math.floor(Math.random() * 99)], ['New York', Math.floor(Math.random() * 99)], ['San Francisco', Math.floor(Math.random() * 99)], ['Portland', Math.floor(Math.random() * 99)] ] } });

the message content looks nothing like a single object that the imp class accepts. I guess I’m asking if anyone can tell me how I should format my data to be compatible.

The imp class’ publish() method accepts a table as data, so you create a table containing the key “columns” and, as its value, the array of cities, and post it. Something like:

`// Create empty table
local data = {};

// Populate a new key,  columns
data.columns <- [
    ['x', time()],
    ['Austin', math.floor((1.0 * math.rand() / RAND_MAX) * 101)],
    ['New York', math.floor((1.0 * math.rand() / RAND_MAX) * 101)]
    // etc.
];

// Send to PubNub
pubnub.publish(channel, data);`

Thanks for the suggestion, I’m still getting to grips with tables (and object oriented programming in general!). However, I still can’t get it to work and it’s hard to know how to debug as the workings at the receive end are hidden in the Eon chart stuff.

One thing I don’t get is the description of the publish call on https://github.com/electricimp/pubnub states:

// Publish an object: pubNub.publish(channelName, { foo = "bar" });

The published data in the second parameter takes the form of an assigment. I don’t really understand why. I cut & pasted some code from the imp example that sends light level to PubNub and that works fine, but again, the publish call is in the form:

pubnub.publish(lightLevelChannel, { light = lightlevel});

And at the subscribe end:
pubnub.subscribe({ channel : "xxxxxxxxxx-lightLevel", message : function(m){ if (m.light <= 25) {...

So light appears to be a key for the single data value. I’m sure I’m getting closer, but in your example above, you just pass a reference for the data table containing several keys. Is this correct?

Squirrel supports two ways of entering a table constant:

{ key = value } and { key : value }

The latter is generally favoured because it’s closer to the JSON format, but a number of older docs still use the former.

I could have written my example above as:

`pubnub.publish(channel, { columns = [
    ['x', time()],
    ['Austin', math.floor((1.0 * math.rand() / RAND_MAX) * 101)],
    ['New York', math.floor((1.0 * math.rand() / RAND_MAX) * 101)]
    // etc.
] } );`

ie. for the data parameter drop in a table with the key columns whose value is an array of arrays.

In my original code, I create the data table separately. So I create an empty table (the curly braces indicate data is a table. Then I add a key called columns, which I set to the array of arrays.

The <- is Squirrel’s operator for creating and assigning a value to a key. Once the key is added to the table, you use the usual = operator to assign a different value.

Yes, I pass a reference to data into the pubnub.publish() function; the pubnub class uses this reference to access the values it needs, which it bundles up as JSON and sends off to PubNub.

Is your pubnub.subscribe() snippet Squirrel code? If so, it’s incorrect. This function takes two parameters: the channel name as a string in an array, and a reference to a function that will receive and process incoming data. The function has a very specific structure, ie. the parametrs err, result and timetoken, which yours doesn’t.

Thanks for your patient explanation. It would be easier to get the hang of this stuff if the syntax was fixed, and the variable types were more explicit. I used to complain about the exact opposite when I switched from BASIC to C so don’t mind me!

The subscribe snippet isn’t squirrel, but the java script example here:

Re. Basic to C, I know exactly what you mean. Dynamically typed languages do feel a bit like going backwards. If it was up to me it would be optional: add a type when you need it, don’t when you have a variable that could contain anything.