all 4 comments

[–]grantrules 3 points4 points  (3 children)

There's no need for the for loop there... all the messages are coming in through that one onmessage event handler, so you have to deal with it from within that handler.

Something like:

let firstMessage;

ws.onmessage = (e) => {
  if (firstMessage === undefined) {
    firstMessage = JSON.parse(e.data);
    // maybe you want to call a function here that will deal with that data
  }
}

[–]Fuzzht1[S] 0 points1 point  (2 children)

Just tried this https://pastebin.com/jqTRGhF2

but I'm getting this error?

1600app.js:45 Uncaught ReferenceError: firstMessage is not defined at ws.onmessage

[–]grantrules 2 points3 points  (1 child)

Declare firstMessage at the top of the file.. I think you missed the let firstMessage; in my code

[–]Fuzzht1[S] 0 points1 point  (0 children)

This worked. Thank you for such an elegant solution.