Why error when sending?

Error sending.
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable
var connection = new WebSocket('ws://localhost:8080/events/ws/');
 connection.onopen = function () {
 console.log('Connection open!');
};
 connection.onclose = function () {
 console.log('Connection closed');
};
 connection.onerror = function (error) {
 console.log('Error detected:' + error);
};
 connection.onmessage = function (e) {
 var server_message = e.data;
console.log(server_message);
};
 // InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable
 connection.send('Hey server, whats up?');
July 9th 19 at 13:07
1 answer
July 9th 19 at 13:09
Solution
apparently, the socket communication has not been established. Try connection.send put inside connection.onopen
stackoverflow

Find more questions by tags JavaScript