Ajax custom command to clear cache of custom block with flagging


I created a custom block that shows the number of flagged nodes.
Because most hooks for flagging in Drupal 8 are deprecated, I used an event that listens to flagging and unflagging a node. For now when i flag/unflag a node, it automatically clears the cache for the custom block in the sidebar and on next page refresh in show the new number of flagged nodes.
I am now trying to implement automatic refreshing of the block when someone flags/unflags a node. So for this I’ll need Ajax custom commands.
Does anbody have any idea how to implement this?
Thank you