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

Hello World with messaging

ANSWERED

I'm trying to wrap my head around messaging in the fitbit ecosystem. With a simple exercise: 

1) Display a textblock that says "Loading"

2) Send a message with "Hello World" up to my app from the companion

3) Render that message on the gui. 

 

Right now, that's not working at all it just shows the original message and doesn't seem to be doing anything. Am I missing some steps?

 

I have: index.gui

 

<svg class="background">
  <text id="test" x="40%" y="60" text-length="20" text-overflow="ellipsis" >Loading</text>
</svg>

 

 

In app/index.js

 

import document from "document";
import * as messaging from "messaging";

let test= document.getElementById("test");

messaging.peerSocket.onmessage = event => {
  console.log("Data up to the app: " + event);
  test.text = event.data; // Should display message
}

messaging.peerSocket.onerror = (err) => {
  console.log(`Connection error: ${err.code} - ${err.message}`);
}

In companion/index.js:

 

import { peerSocket } from "messaging";
if (peerSocket.readyState === peerSocket.OPEN) { peerSocket.send("Hello World"); // Should send message to app } peerSocket.onerror = (err) => { console.log(`Connection error: ${err.code} - ${err.message}`); }

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Got it! 

In the companion I needed to wrap my send(..) in a onopen() method. Can't send stuff without it being open:

import { peerSocket } from "messaging";
peerSocket.onopen = () =>{
if (peerSocket.readyState === peerSocket.OPEN) {
   peerSocket.send("Hello World");
}
}
peerSocket.onerror = (err) => {
  console.log(`Connection error: ${err.code} - ${err.message}`);
}

 

View best answer in original post

Best Answer
1 REPLY 1

Got it! 

In the companion I needed to wrap my send(..) in a onopen() method. Can't send stuff without it being open:

import { peerSocket } from "messaging";
peerSocket.onopen = () =>{
if (peerSocket.readyState === peerSocket.OPEN) {
   peerSocket.send("Hello World");
}
}
peerSocket.onerror = (err) => {
  console.log(`Connection error: ${err.code} - ${err.message}`);
}

 

Best Answer