How to send stored chat history from Websocket on page load?

Using a Javax/Jakarta Websocket for a group chat application and I am storing each group’s message history in a ConcurrentList<String>. I want to send users all the old messages whenever they open up the chat.jsp page. Unfortunately, it seems the websocket’s onOpen() only runs on websocket creation and doesn’t run each time the page is opened. This means each time my users refresh chat.jsp, it clears all the messages in the chat box.

What would be an effective way to perform chatHistory.get(group).forEach(m -> curSession.getAsyncRemote().sendText(m)); each time the user opens up the chat page? Is there some event I can utilize here either client or server side? I know there is a client onLoad event but I’m not sure how I would ask the server to send the message history data when that event fires.

ChatServerEndpoint.java

@ServerEndpoint("/message")
public class ChatServerEndpoint {

// Key: unique group code, Value: Sessions of users in that group
private static ConcurrentHashMap<String, Set<Session>> groupSessions = new ConcurrentHashMap<String, Set<Session>>();

// Key: unique group code, Value: list of messages in order sent
private ConcurrentHashMap<String, List<String>> chatHistory = new ConcurrentHashMap<String, List<String>>();

private String group;

@OnOpen
public void onOpen(Session curSession) {
    // Get group code from user session
    group = curSession.getRequestParameterMap().get("group").get(0);
    
    // Add user to group session
    if (!groupSessions.containsKey(group)) {
        groupSessions.put(group, Collections.synchronizedSet(new HashSet<Session>()));
    }
    groupSessions.get(group).add(curSession);
    
    // Start chat history if doesn't exist for this group
    if (!chatHistory.containsKey(group)) {
        chatHistory.put(group, Collections.synchronizedList(new ArrayList<String>()));
    } else {
        // Send old messages to client
        chatHistory.get(group).forEach(m -> curSession.getAsyncRemote().sendText(m));
    }
}
        
@OnClose
public void onClose(Session curSession) {
    // Remove user from group session
    if (groupSessions.get(group).size() == 1) {
        groupSessions.remove(group);
    } else {
        groupSessions.get(group).remove(curSession);
    }
}

@OnMessage
public void onMessage(String message, Session userSession) {
    chatHistory.get(group).add(message);
    for (Session ses : groupSessions.get(group)) {
        ses.getAsyncRemote().sendText(message);
    }
}

}

chat.js

var wsUrl;
if (window.location.protocol == 'http:') { wsUrl = 'ws://'; }
else { wsUrl = 'wss://'; }

var socket = new WebSocket(wsUrl + window.location.host + "/MyProject/message");
        
socket.onmessage = function(event) {
    var mySpan = document.getElementById("chat");
    mySpan.innerHTML += event.data;
};
     
socket.onerror = function(event) {
    console.log("Chat Error ", event)
};

function sendMsg() {
    var msg = document.getElementById("msg").value;
    if (msg) {
        socket.send(msg);
    }
    document.getElementById("msg").value = "";
}