# Websocket Stops Sending Data - No errors

**URL:** <https://community.tempest.earth/t/websocket-stops-sending-data-no-errors/26375>\
**Category:** Developers\
**Tags:** websocket, tempest\
**Created:** [January 9, 2025, 5:17pm UTC](https://community.tempest.earth/t/websocket-stops-sending-data-no-errors/26375 "2025-01-09T17:17:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pickusj](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@pickusj](https://community.tempest.earth/u/pickusj)\
**Post date:** [January 9, 2025, 5:17pm UTC](https://community.tempest.earth/t/websocket-stops-sending-data-no-errors/26375/1 "2025-01-09T17:17:00Z")

</div>

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:

```auto
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);
	}
  });
});

```

---

<div class="post-metadata">

**Author:** ![pickusj](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@pickusj](https://community.tempest.earth/u/pickusj)\
**Post date:** [January 10, 2025, 7:05pm UTC](https://community.tempest.earth/t/websocket-stops-sending-data-no-errors/26375/2 "2025-01-10T19:05:44Z")

</div>

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).

```auto
/*

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()
		
}
	
	

```

---

<div class="post-metadata">

**Author:** ![peter](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/peter/32/1365_2.png) [@peter](https://community.tempest.earth/u/peter)\
**Post date:** [January 10, 2025, 9:31pm UTC](https://community.tempest.earth/t/websocket-stops-sending-data-no-errors/26375/3 "2025-01-10T21:31:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pickusj](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@pickusj](https://community.tempest.earth/u/pickusj)\
**Post date:** [January 10, 2025, 10:45pm UTC](https://community.tempest.earth/t/websocket-stops-sending-data-no-errors/26375/4 "2025-01-10T22:45:04Z")

</div>

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