Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem Sending Multiple Messages from Companion to Device

ANSWERED

Why is this illegal:

 

app/index.js

messaging.peerSocket.onmessage = evt => {
    myWater.text = `${Math.ceil(evt.data.water/29.574)} oz`;
    myCalories.text = `${evt.data.calories}`;
};

companion/index.js

 if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
      messaging.peerSocket.send(myWaterData);
      messaging.peerSocket.send(myCalorieData);
    }

 

Calories show up fine but Water shows up as NaN. There has to be a way to batch-send/receive several values from the Web API to the device.

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

I figured that that specific function had a "data" element so I tried seeing if the message would accept all of the declared data within the function and it worked!

.then(function(data) {
    let calorieGoalData = {"goals":data.goals};
    let summary = {"summary":data.summary};
    if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
      messaging.peerSocket.send(data);
    }

 

View best answer in original post

Best Answer
0 Votes
6 REPLIES 6

You seem to be sending two separate messages, rather than one message that includes both values.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

What should the format be to send both values in one message? Using commas? Turning them into an array? And would the format in app.index.js still be valid?

 

Thanks! this is literally the last step before my watch app is completed!

Best Answer
0 Votes

I'd probably use an object, but an array would also work.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

I realized that the food summary included both water and calorie data so that worked nicely! But what if I were to send separate data points like "calories" and "calorie goal" data in one message, how would I do that? Or sending separate databases like sleep and nutrition in one message even.

Best Answer
0 Votes

What would that look like? What's the syntax?


@Gondwana wrote:

I'd probably use an object, but an array would also work.


 

Best Answer
0 Votes

I figured that that specific function had a "data" element so I tried seeing if the message would accept all of the declared data within the function and it worked!

.then(function(data) {
    let calorieGoalData = {"goals":data.goals};
    let summary = {"summary":data.summary};
    if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
      messaging.peerSocket.send(data);
    }

 

Best Answer
0 Votes