06-23-2022 06:38
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-23-2022 06:38
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
In companion
//set up a socket to send data
import * as messaging from "messaging";
//fetch user data first
const wsUri = "ws://127.0.0.1:8080"
console.log("creating websocket")
var websocket = new WebSocket(wsUri);
websocket.addEventListener("open", onOpen);
//websocket.addEventListener("close", onClose);
websocket.addEventListener("message", onMessage);
websocket.addEventListener("error", onError);
function onOpen(evt) {
console.log("CONNECTED");
}
function onClose(evt) {
console.log("DISCONNECTED");
}
function onMessage(evt) {
console.log(`MESSAGE: ${evt.data}`);
}
function onError(evt) {
console.error(`ERROR: ${evt.data}`);
}
//open socket to fitbit device
messaging.peerSocket.addEventListener("open", (evt) => {
console.log("Ready to send or receive messages");
});
messaging.peerSocket.addEventListener("message", (evt) => {
//post data to our kafka consumer
console.log(JSON.stringify(evt.data));
const data = JSON.stringify(evt.data)
websocket.send(data)
});
as soon as data is received and sent the socket disconnects.
[4:37:23 PM] Companion: Ready to send or receive messages (companion/index.js:33,3)
[4:37:23 PM] Companion: CONNECTED (companion/index.js:15,5)
[4:37:27 PM] Companion: {"hr":166,"timestamp":2429038522} (companion/index.js:39,5)
[4:37:27 PM] Companion: DISCONNECTED
is there something I need to do to prevent this disconnection?
Thanks in advance!
Answered! Go to the Best Answer.

Accepted Solutions
06-24-2022 00:57
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-24-2022 00:57
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
ok, I solved this and its nothing to do with FitBit SDK! It was dependent on how you implement the websocket server. Here is a simple websocket server that works.
from simple_websocket_server import WebSocketServer, WebSocket
class SimpleEcho(WebSocket):
def handle(self):
# echo message back to client
print(self.data)
#self.send_message(self.data)
def connected(self):
print(self.address, 'connected')
def handle_close(self):
print(self.address, 'closed')
server = WebSocketServer('', 8080, SimpleEcho)
server.serve_forever()

06-24-2022 00:57
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-24-2022 00:57
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
ok, I solved this and its nothing to do with FitBit SDK! It was dependent on how you implement the websocket server. Here is a simple websocket server that works.
from simple_websocket_server import WebSocketServer, WebSocket
class SimpleEcho(WebSocket):
def handle(self):
# echo message back to client
print(self.data)
#self.send_message(self.data)
def connected(self):
print(self.address, 'connected')
def handle_close(self):
print(self.address, 'closed')
server = WebSocketServer('', 8080, SimpleEcho)
server.serve_forever()

06-24-2022 01:18
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-24-2022 01:18
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
this can be dockerised with
FROM python:3.8-slim
# App setup
COPY . /app
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install simple-websocket-server
EXPOSE <your_port>
CMD ["python", "-u", "<your_sever>.py"]
but update this line in the socket server
server = WebSocketServer('0.0.0.0', 8080, SimpleEcho)
to run it locally.

