How to join multiple rooms by just sending one <presence> message to ejabberd server

让人想犯罪 __ 提交于 2019-12-06 12:27:09

问题


For example, I have 20 rooms to join. The simple solution is to send 20 message to each room id. Considering the performance, this is bad.

I want to join the 20 rooms by just sending one <presence> message, how to achieve this? Writing a module to hook the custom <presence> message? But I do not know how to write this kind of module.


回答1:


In XEP-0045 Multi User Chat, there is no way defined to join 20 chat rooms with a single presence packet.

However, by combining other XMPP Extension with Multi User Chat, you can definitely achieve this in pure XMPP, without the need to write custom ejabberd extensions.

You can rely on XEP-0033 Extended Stanza Addressing, you can send an XMPP packet to several recipient. It also works with presence as shown in this example.

ejabberd configuration

XEP-0033 Extending Stanza Addressing is supported as default since ejabberd 15.04. Make sure you have enable the feature by adding mod_multicast in ejabberd configuration modules section:

modules:
...
  mod_multicast: {}

When the service is enabled, you should have a new service (as default named multicast.example.net) on the server supporting the feature http://jabber.org/protocol/address:

<iq type='get'
    to='multicast.example.net'
    id='info1'>
  <query xmlns='http://jabber.org/protocol/disco#info'/>
</iq>

and response is:

<iq from="multicast.example.net" type="result" to="test@example.net/laptop" id="info1">
<query xmlns="http://jabber.org/protocol/disco#info">
<identity category="service" type="multicast" name="Multicast"/>
<feature var="http://jabber.org/protocol/disco#info"/>
<feature var="http://jabber.org/protocol/disco#items"/>
<feature var="vcard-temp"/>
<feature var="http://jabber.org/protocol/address"/>
</query>
</iq>

Usage

Once enabled, it is easy to send a presence packet targeting several MUC rooms:

<presence to='multicast.example.net'>
    <addresses xmlns='http://jabber.org/protocol/address'>
        <address type='bcc' jid='testroom@conference.example.net/Usernick'/>
        <address type='bcc' jid='testroom2@ conference.example.net/Usernick'/>
    </addresses>
</presence>

You will thus see that you are joining several rooms at the same time.

Issues

XEP-0033 did not specifically mention that use case and the module had not been testing to join multiple rooms. While writing this example I found that when disconnecting, user does not properly leave the rooms he had joined. It means you have to wait for the following Github issue to be fixed before the feature is usable in production: Broadcast presence change after multicast presence packet.



来源:https://stackoverflow.com/questions/31781137/how-to-join-multiple-rooms-by-just-sending-one-presence-message-to-ejabberd-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!