title: Plotly and Websockets
body: <p>Currently I am trying to use Plotly to make a live pie chart with data coming in from a websocket connection. However, I cant seem to get the data from the socket to the graph. Right now I see two ways of doing it. One is to get the data out of the onmessage function, such as returning a value to the parent function. The other is putting the Plotly code into the onmessage function. Neither are currently working. I'm not planning on talking about getting data out of the onmessage function in this thread since this one is for Plotly not websocket. I will link a stack overflow thread where I asked about the websocket problem if your interested in that part.</p>
  <p><a href="https://stackoverflow.com/questions/65960341/javascript-getting-values-out-of-websocket-functions">JavaScript: Getting Values out of Websocket Functions</a></p>
  <p>So, Onto the problems I’m running into with putting Plotly inside the onmessage function. For some reason, when I cut and paste the code to create the graph into that function it “freezes” the program. The script will run as normal till it gets to the point where it makes the graph, you can see it make the graph, then it just sits there. The entire code stops as shown by the fact that it should loop back to the start of the function and update live text on the page and this stream stops. My code is given below if you want to take a look at it.</p>
  <pre><code>var pmsg = &quot;null&quot;;
  var ws = new WebSocket('ws://localhost:10011/');
  ws.onmessage = function(event)
  {
       if(pmsg == 'QC1R1TR')
       {
            QC1R1TR = event.data;
            var testing = document.getElementById('QC1R1TR');
            testing.innerHTML = &quot;Run Time: &quot; + QC1R1TR;
       }
   
       pmsg = event.data;
  
       var data = [{
       values: [2, 1],
       labels: ['TR', 'TNR'],
       type: 'pie',
       automargin: true
       }];
  
       var layout = {
       height: 300,
       width: 300,
       margin: {&quot;t&quot;: 0, &quot;b&quot;: 0, &quot;l&quot;: 0, &quot;r&quot;: 0},
       //showlegend: false
       };
  
       Plotly.newPlot('QC1R1Pie', data, layout);
  }
  </code></pre>
  <p>Please note that when the plot is static numbers currently so its not an issue with the variables. The plot works when its in the parent function and so does the live text on the page.</p>
  <p>Any help with this would be greatly appreciated!</p>
  <p>Thanks,
  Luke</p>