So I am building a custom web chat interface for a client who really hates the default NICE CXone Messenger widget. They want total control over the UI, so I am using the Guest API directly to handle the messaging. The basic flow works fine. I can get a session token, send a message, and receive the reply. But I am running into a weird issue with message ordering and session persistence when the user navigates away from the page.
Here is what I am doing. First, I hit the POST /api/v2/guest/webmessaging/sessions endpoint to get a session. I store the webMessagingSessionId in local storage. Then I use POST /api/v2/guest/webmessaging/sessions/{sessionId}/messages to send text.
The problem is when the page reloads. I try to resume the session using the stored ID. The API call to GET /api/v2/guest/webmessaging/sessions/{sessionId}/messages returns the history, but it seems to be missing the last few messages sent just before the reload. Also, if I send a message immediately after reload, it sometimes goes through, and sometimes I get a 409 Conflict error saying the session is locked or invalid.
Is there a specific way to handle the session state in the Guest API? Do I need to poll for updates differently? I tried using the WebSocket endpoint wss://api.mypurecloud.com/v2/guest/webmessaging/events, but it disconnects randomly if the network hiccups.
Here is a of my resume logic:
async function resumeSession(sessionId) {
const response = await fetch(`/api/v2/guest/webmessaging/sessions/${sessionId}/messages`, {
headers: { 'Authorization': `Bearer ${token}` }
});
if (response.status === 409) {
// What to do here? Create new session?
}
return response.json();
}
I feel like I am missing a step in the handshake. Does the Guest API require a specific keep-alive ping? Or is the 409 expected behavior and I should just create a new session every time?