Update widget when new message received?

I have a theme component widget that displays a little bubble when there is a New or Unread topic. To get it to update in real time I do this:

  topicTrackingState.onStateChange(() => {
    this.scheduleRerender();
   }); 

I would like to do something similar with PMs. I can use new_personal_messages_notifications_count to get the number of messages for a user. But if a new message is received the widget does not automatically rerender. Is there an easy way for me to update the widget on a new message being received similar to the topicTrackingState above?

So I found a working solution. There is apparently a private message version of the TopicTrackingState. Took me a while to figure out that it requires a “key” as a parameter, which is unclear to me what the key is for but it took an arbitrary string without issue

  pmTopicTrackingState.onStateChange("pm-tracking-key", () => {
    this.scheduleRerender();
   });

This triggers the rerender but I noticed that new_personal_messages_notifications_count is not updated in time for the rerender so it fails to actually update immediately.

I think this is a “good enough” solution for me though. The notification eventually makes it’s way onto the widget. I’m happy to hear any comments on this though

2 Likes