Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

345
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!