otDiv = document.createElement("div");
            botDiv.className = "bot";
            botDiv.innerHTML = formattedBuffer;
            chatDiv.appendChild(botDiv);
          }

          scrollToBottom();
        } catch (e) {
          console.error("Error while handling WebSocket message:", e);
        }
      };

      socket.onerror = function(error) {
        console.error("WebSocket Error:", error);
      };

      socket.onopen = function() {
        console.log("WebSocket connection established.")