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

send message using messaging API (websocket API)

I want to send a message "fall" or "Not detection" to my 3rd part app. 
I used companion using messaging API.
But I have a problem with make a print on APP.

SO I want to make print on any HTTP. 
How can i make a source???
Under is my companion code. 

Do I have to make a server? 
Can I make a print on my local http? 
Or Can I send data to my app's DB?

 

import * as messaging from "messaging";

messaging.peerSocket.onopen = () => {
  console.log("Ready");
  sendMessage();
}

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

messaging.peerSocket.onmessage = (evt) => {
  console.log(JSON.stringify(evt.data));
}

function sendMessage() {
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
    // Send the data to peer as a message
    messaging.peerSocket.send({
      sampleData: 123456
    });
  }
}

import { me } from "companion";

if (!me.permissions.granted("access_internet")) {
  console.log("We're not allowed to access the internet!");
}

/*
var connection = new WebSocket('ws://127.0.0.1')
 
connection.onopen = function(){
  //Send a small message to the console to signify connection is made.
  console.log('Connection open!');
}
 
connection.onclose = function() {
  console.log('Connection closed!');
}
 
connection.onerror = function(error) {
  console.log("Error detected: " + error);
}
*/
const wsUri = "wss://localhost:8000/";

let websocket;

function testWebSocket() {
  websocket = new WebSocket(wsUri);
  websocket.addEventListener("open", onOpen);
  websocket.addEventListener("close", onClose);
  websocket.addEventListener("message", onMessage);
  websocket.addEventListener("error", onError);
  websocket.addEventListener("send", doSend);
}

function onOpen(evt) {
  console.log("CONNECTED");
  doSend("WebSocket rocks");
}

function onClose(evt) {
  console.log("DISCONNECTED");
}

function onMessage(evt) {
  console.log(`MESSAGE: ${evt.data}`);
  websocket.close();
}

function onError(evt) {
  console.error(`ERROR: ${evt.data}`);
}

function doSend(message) {
  console.log(`SEND: ${message}`);
  websocket.send("sent");
}

testWebSocket();

 

Best Answer
0 Votes
1 REPLY 1

You can read more about how communications work here https://dev.fitbit.com/build/guides/communications/

 

 

Best Answer
0 Votes