Websocket Stops Sending Data - No errors

When i open a websocket, data stops being sent after about 15 minutes. There is no errors being reported, that i can tell. Basically it just hangs. This happens everytime i start a websocket. Data does flow immediately after i start the socket, but then after a short period (15 min or so) it just hangs and fails to report any more data. I am using node.js, My code:

const fs = require("fs");   // file stuff

// open websocket to tempest server using my token
const WebSocket = require('ws');
const ws = new WebSocket("wss://ws.weatherflow.com/swd/data?token=<my token>);

// construct data to start listening
const data = {
	"type": "listen_start",
	"device_id": 12345,
	"id": "jdp-1"
	}

const jContent = JSON.stringify(data);


// open websocket and send data to start getting weather data 
ws.on('open', () => {
  console.log('Connected to Tempest');
  ws.send(jContent);
});


// receive information from websocket and display to console
ws.on('message', (data) => {
  const rd = JSON.parse(data);      // make data readable
  console.log('Received My Data:', rd);
  fs.writeFile("wdata.txt",JSON.stringify(rd),(err) => {
	if (err) {
		console.error("Error writing to file:",err);
	}
  });
});

I was able to reconstruct the Websocket to restart after a set period to ensure that i continue to receive data. My experience is that the Tempest websocket (based on how i opened it) will hang and stop sending weather data (no errors or close event) after a short period (5-15 min).

/*

Creates websocket to Tempest and saves latest weather data to a file.
Websocket will be closed after 8 min to account for tempest websocket timeout issue. (issue where open websocket stops sending weather data 
after a short time (10-15 min)). 
Interval is used to close socket.  

Websocket "onclose" event reconnect websocket after a short wait. 

jpickus 01-10-2025

*/


const fs = require("fs");   // file stuff
let ws; 
let intervalId;   // global variable for interval 
openSocket()


function closeSocket() {
	clearInterval(intervalId);
	if (ws && ws.readyState === WebSocket.OPEN) {
		ws.close(1000,"Normal Close");
	}
}


function startInterval() {
	console.log("Starting Interval...");
	intervalId = setInterval(() => {
		closeSocket();
	},500000);
	}

function reconnect() {
	setTimeout(() => {
		console.log('Attempting to reconnect...');
		openSocket();
	}, 10000);   // retry after 10 seconds
}

function openSocket() {
	console.log('Starting openSocket....');
	WebSocket = require('ws');
	ws = new WebSocket("wss://ws.weatherflow.com/swd/data?token=<token>");

	ws.onclose = (event) => {
	  console.log('Connection closed XXX');
	  reconnect();
	};
	
	ws.onerror = (error) => {
		console.error('WebSocket error:', error);
		reconnect(); // Attempt to reconnect on error
    };

	
	// construct data to start listening
	const data = {
		"type": "listen_start",
		"device_id": <device id>,
		"id": "jdp-1"
		}

	const jContent = JSON.stringify(data);
	
	
	// open websocket and send data to start getting weather data 
	ws.onopen = () => {
	  console.log('Connected to Tempest');
	  ws.send(jContent);
	};


	// receive information from websocket and display to console
	ws.on('message', (data) => {
	  const rd = JSON.parse(data);      // make data readable
	  console.log('Received My Data:', rd);
	  fs.writeFile("wdata.txt",JSON.stringify(rd),(err) => {
		if (err) {
			console.error("Error writing to file:",err);
		}
	  });
	});

	startInterval()
		
}
	
	

I often see this too. I use a similar approach to you to close and reopen the connection but only once the data has stopped flowing.

Your approach is better, thank you. Will research how best to catch this (data not being received). Can you share your approach?