Friends Count?

Help creating a user counter plugin for Elgg 6.2

Hello everyone,

I am working on an Elgg 6.2 community website and I would like some help creating a plugin that stores user counters directly in the user entity.

The idea is to avoid repeatedly counting friends and groups by loading all pages. Instead, I would like the plugin to maintain counters that are updated automatically whenever something changes.

For example:

  • When a user gains or loses a friend, update a friends_count value.
  • When a user joins or leaves a group, update a groups_count value.
  • Store these values as user metadata or another recommended Elgg method.

Then, on the frontend, I would only need to display these stored values:

FRIENDS (153)
GROUPS (8)

without making additional queries or scanning pagination pages.

My questions are:

  1. What is the recommended Elgg 6.2 approach for storing these counters?
    • User metadata?
    • A custom database table?
    • Another Elgg API method?
  2. Which Elgg events/hooks should I use to update the counters?
    • Friend relationships being created/deleted.
    • Group membership being created/deleted.
  3. What would be the best way to expose these values to views or JavaScript on the frontend?

I am trying to build this in a way that is efficient and follows Elgg's best practices, especially considering performance on a growing community.

Any examples, suggestions, or guidance would be greatly appreciated.

Thank you!