0
votes

For my thesis, I am using Smack to log a XMPP network that uses the MUC module.

Another software is currently sending IoT sensor data into different MUC rooms.

I'd like to know for every message sent into a MUC room, which users were in that room at the time of the message. Is this possible? I could use a messageListener to every muc room, however the listener only receives a message as an argument. Therefore I could not know who is logged into the room inside the listener method.

2

2 Answers

0
votes

you can get all muc message in StanzaListener in xmpp. Please follow few steps to done this

Step 1. Declare as a global variables

ChatManagerListener chatListener;
Chat chat;
StanzaListener packetListener;

Step 2. Use this code in oncreate or in fragment

Note: Make sure you have connected with chat server.

    packetListener = new StanzaListener() {
        @Override
        public void processPacket(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {

            if (packet instanceof Message) {
                final Message message = (Message) packet;

               }
        }
    };

        XMPP.getInstance().getConnection(acitiviy)).addAsyncStanzaListener(stanzaListener, null);

    ServiceDiscoveryManager sdm = ServiceDiscoveryManager
        .getInstanceFor(XMPP.getInstance().getConnection(acitiviy)));
    sdm.addFeature("jabber.org/protocol/si");
    sdm.addFeature("http://jabber.org/protocol/si");
    sdm.addFeature("http://jabber.org/protocol/disco#info");
    sdm.addFeature("jabber:iq:privacy");

Step 3. Methods for one to one chat purposer

void sendMessage(String message) {
if (chat != null) {
    try {
        chat.sendMessage(message);
            Message msg = new Message();
            msg.setTo(JidCreate.bareFrom(jid));
            msg.setFrom(XMPP.getInstance().getConnection(acitiviy)
                    .getUser());

            ChatStateExtension ext = new ChatStateExtension(
                    ChatState.paused);
            msg.addExtension(ext);
            lastComposing = System.currentTimeMillis();
            chat.sendMessage(msg);

    } catch (SmackException.NotConnectedException e) {
    } catch (Exception e) {

    }
}

}

Step 4. On destroy XMPP.getInstance().getConnection(acitiviy)).removeAsyncStanzaListener(stanzaListener);

Hope this will help you and if you want more information take a look from here. Thankyou

0
votes

Nothing prervents you from calling Multi UserCaht.getParticipants() from within the listener. But be warned: If your goal is to determine the other receivers of receivers, then this approach is fragile. I also suggest to think about using PubSub instead of MUC for your IoT use case.