Hello. I'm trying to post current location to a web api on button press from app/index.js. The fetch function is in companion/index.js. I can call the function successfully from the companion file, but I don't know how to access the companion function on button press in the app/index.js file. How do I do this? Or is it a better way to solve this problem?
app/index.js:
/*
* Entry point for the watch app
*/
import * as document from "document";
import { geolocation } from "geolocation";
import { me as companion } from "companion";
const myButton = document.getElementById("button");
myButton.addEventListener("click", (evt) => {
//call post function from here.
geolocation.getCurrentPosition(locationSuccess, locationError, {
timeout: 60 * 1000
});
})and companion/index.js:
/*
* Entry point for the companion app
*/
console.log("Companion code started");
// Companion
function postLocation(location) {
fetch('https://localhost:60825/api/User', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ "location": location })
})
}
Best AnswerI recommend 'file transfer' from device to companion. The device click listener would initiate the transfer. When the companion receives the file, it would call postLocation().
Messaging would be slightly faster, but is less reliable.