export default function createWebSocket(url, token, onMessageCallback) {
  let socket;
  let retries = 0;
  const maxRetries = 5
  let messageQueue = [];

  function connect() {

      socket = new WebSocket(url, [token]);

      socket.addEventListener('message', (event) => {
          const jsonData = JSON.parse(event.data)
          onMessageCallback(jsonData)
      });

      socket.onopen = () => {
          //console.log('WebSocket is open .')
          //console.log(socket)

          while (messageQueue.length > 0) {
            socket.send(messageQueue.shift());
        }
      };

      socket.onclose = (event) => {
          //console.log('WebSocket is closed .', event)
      };

      socket.onerror = (error) => {
          //console.error('WebSocket error observed:', error)
      };

      
  }

  connect()

  return socket
}
