使用ejabberd API添加MUC成员后,成员数显示为0的问题咨询
get_room_occupants_number Returns 0 After Subscribing Users via subscribe_room Hey there! Let's clear up the confusion here—this behavior is actually expected, and it comes down to a key distinction between MUC-Sub subscriptions and room occupants in ejabberd.
1. The Core Difference: Subscriptions vs. Occupants
When you use the subscribe_room API, you're setting up a MUC-Sub subscription for the user. This lets them receive updates from the room (like new messages, affiliation changes, or subject updates) without needing to be actively connected or joined to the room.
However, an "occupant" is defined as a user who is currently online and has explicitly joined the room via an XMPP MUC join request. Subscribing doesn't automatically make a user an occupant—these are two separate concepts entirely.
2. What You're Missing: Actual Room Joining
To have users count towards the occupants number returned by get_room_occupants_number, you need to ensure the user actively joins the room. Here's how to do this:
- Via XMPP Client: Have the user's client send a MUC join stanza to the room (e.g., a presence stanza directed to
roomdub@conference.dub/vish). - Via ejabberd API: If you need to trigger this server-side, you can use the
send_stanzaAPI to simulate a join request. For example:{ "from": "vishesh@dub/dummy", "to": "roomdub@conference.dub/vish", "stanza": "<presence/>" }
3. Verify Subscriptions (Instead of Occupants)
If you want to confirm that your subscribe_room calls worked correctly, use the get_room_subscribers API instead. This will return the list of users who have subscribed to the room's updates:
- Request:
{ "room": "roomdub@conference.dub" } - Expected Response:
You should seevishesh@dub/dummylisted here, confirming the subscription was successful.
4. Quick Room Configuration Check
Double-check your room's configuration to rule out any edge cases:
- Ensure the room is marked as
persistent(you already did this, which is good!). - If the room is set to
members_only, users need to have an affiliation (likemember) before they can join. You can set affiliations using theset_room_affiliationAPI if needed.
Summary
The get_room_occupants_number metric only counts users who are actively joined and online in the room. Your subscriptions are working (as confirmed by the subscribe_room response), but those users aren't counted as occupants until they join the room.
内容的提问来源于stack exchange,提问作者Vishesh Madhusudhana

