I tried to do it this way, but of course, it doesn't work
this.connection.onmessage = event => {
setInterval(() => {
console.log(event.data)
}, 3000)
}
A Websocket is designed to be event driven.
When the server sends data to your client, the onmessage event runs.
What you have done with this code, is to tell client to output the message received from the server every 3 seconds, every time a message is received.
To output data every 3 seconds, the data should be sent every 3 seconds otherwise you are missing the point of Websockets.