01-14-2025 05:15 AM
Hi everyone,
It's a long one; apologies in advance!
I'm working on a Webex Embedded App using the sample Kitchen Sink App to test a message counter badge within the sidebar. I’ve run into some challenges:
To test this, I’ve added a custom getMessage() button. Here’s the logic for the message counter and button:
/**
* Simulates receiving a new message and updates the unread message counter in Webex sidebae
*/
const setUnreadMsgCounterBadge = async (unreadMsgCount) => {
try {
const unreadCount = Object.values(
store.getState()?.roomsPageReducer?.unreadInboxRooms || {},
).reduce((a, c) => a + c, 0);
const count = unreadMessagesCount ?? unreadCount;
if (isNaN(count) || count < 0) {
console.error("Invalid count value:", count);
return;
}
if ("serviceWorker" in navigator) {
return navigator?.serviceWorker?.controller?.postMessage({
type: "set_unread_message_counter",
unreadCount: count,
});
}
const webexSidebar = await webexApplication.context.getSidebar();
const res = await webexSidebar.showBadge({ badgeType: "count", count });
log("Debug: Badge update response", res);
} catch (e) {
console.error(
log("Badge Update Failed", { error: e.message })
);
}
}
webexApplication.onReady().then(async () => {
try {
await setUnreadMsgCounterBadge();
} catch (error) {
console.error("Error invoking setUnreadMsgCounterBadge:", error);
}
});
function handleNewMessage() {
app.context
.getSpace()
.then((space) => {
log("getSpace()", space);
const spaceId = space.id;
const simulatedMessage = {
id: `msg-${Date.now()}`,
spaceId: "spaceId",
text: "This is a test message",
created: new Date().toISOString(),
};
log("New Message Event", simulatedMessage);
try {
const unreadMessagesCount = (window.unreadMessagesCount || 0) + 1;
window.unreadMessagesCount = unreadMessagesCount;
setUnreadMsgCounterBadge(unreadMessagesCount);
log("Updated Message Counter", { unreadMessagesCount });
} catch (error) {
log("Error Handling New Message", { error: error.message });
}
})
.catch((error) => {
log(
"getSpace() promise failed with error",
Webex.Application.ErrorCodes[error]
);
});
}
I can see the simulated messages in the console and so can confirm the handleNewMessage() function is triggered. However, I’m unsure:
The link for the github repo is: https://github.com/mozzarelli3/EmbeddedAppKitchenSink
Any guidance would be greatly appreciated, thanks!
01-14-2025 07:14 AM
@eparry not sure how well that code would work in production for counting messages in a space, haven't tested such a function before, but just something to consider here. We have a JS SDK function that can return you the memberships in a space and details about when the user was last seen. Alternatively you can experiment with the "seen" websocket also provided in the same SDK here https://github.com/webex/webex-js-sdk/blob/1133cbcac1eda2740dccc269e7b87d3c5d5a41a2/packages/%40webex/plugin-memberships/src/memberships.js#L67 . They could be used to display if the logged in user has unread messages in a space, it might help you.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide