Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

344
Vistas
Updating component realtime based on Vue + apexchart + websocket

I have a chart like so:

.....
      <div class="flex flex-auto">
        <apexchart
          width="300%"
          height="380"
          type="line"
          :options="optionsLine"
          :series="seriesLine"
        ></apexchart>
.....

And the vue component has a method like so:

    methods: {
      getTraceData: function (data) {
       var esocket = new WebSocket(
        "ws://localhost:5001/provideTraceData?serverHost=" + data
      );
      esocket.onmessage = function (event) {
        var res = JSON.parse(event.data);
        var ts = [];
        var dataLength = res["timeseries"].length;

        console.log(res);
        for (var i = dataLength - 1; i > dataLength - 11; i--) {
          ts.push([
            res["timeseries"][i]["timestamp"],
            res["timeseries"][i]["ttfb"],
          ]);
        }
        console.log("==", ts);

        this.optionsLine = {
          tooltip: {
            shared: false,
            x: {
              formatter: function (val) {
                var date = new Date(val * 1000);
                // Hours part from the timestamp
                var hours = date.getHours();
                // Minutes part from the timestamp
                var minutes = "0" + date.getMinutes();
                // Seconds part from the timestamp
                var seconds = "0" + date.getSeconds();

                // Will display time in 10:30:23 format
                var formattedTime =
                  hours + ":" + minutes.substr(-2) + ":" + seconds.substr(-2);
                return formattedTime;
              },
            },
          },
        };

        this.seriesLine = [
          {
            name: "TTFB",
            data: ts,
          },
        ];
      };
   }
}

From the WebSocket server I get a JSON response, I parse it, etc, etc, and then try to update the chart series. But for some reason, the chart does not seem to render. But when I use a basic HTTP endpoint without WebSocket to get the same data then the chart renders. I'm sort of new javascript, is there some behind the scene async shenanigans happening I'm unaware of? The logic of updating the chart is same, the difference between the HTTP and WebSocket versions is basically the URL string and the event handler, the rest is the same.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda