powered by Jive Software

MUCLight how to listen for incoming group chat message

@Paul_Schaub
I am using smack 4.2.4 and, implementing the feature of group chat, i am able to send the messages into the group, but unable to receive them, via ChatMessageListener , and even not getting the message on the other users devices using

StanzaTypeFilter filter = new StanzaTypeFilter(Message.class);
connection.addSyncStanzaListener(new StanzaListener() {
@Override
public void processStanza(Stanza packet) throws NotConnectedException, InterruptedException, SmackException.NotLoggedInException {
Log.e(“Group message/n content”, “” + packet.toXML());
}
}, filter);

so whats the correct way to achieve this function?

Are you using the muclight code for messaging? Note that MucLight != Muc.

If you are using muclight, then you probably want to register message listeners via

multiUserChatLight.addMessageListener(messageListener);

If however you are using “normal” muc chats, please use

multiUserChat.addMessageListener(messageListener);

There are convenience methods for most use cases already present in Smack. It is rarely needed to register raw stanza listeners.

but the thing is, this is for a specific group, because multiUserChatLight instance will be obtained only via, roomJid, so how to tackle this if i have more then one group?

You have to register one listener per group.

but, by that way how will I receive messages when the app is in background or not running?

Obviously you want to have a background service which has the XMPPConnection and a bunch of listeners.

The app has however to be running at least in the background.

I have a service, that handles the connection and, messages as well, but when the app is destroyed, whenever i have notification from firebase I tend to start those services in background back again, but if i make the individual listeners for every group, it will make the service more error prone, because service has to discover all the groups once again and, add listeners again every time service starts, which will make the realtime ness of message delivery a bit slow i think

Ideally the service is started only once.

but when the user force full closes the app, it would destroy all the services of the application too, and now, i have no access to get the messages if anyone send me the message

That’s true, but also expected.

what messaging applications like, whatsapp , hike, viber and many others, have the functionality integrated

Those apps use Firebase. I’m not sure how they implemented the functionality, but I guess they fetch new messages from the server once a firebase event has been received.

whatsapp uses XMPP, for messaging and other things
I have looked into their logs, and seen alot of xml packets for presence, and chat states

Whatsapp uses a heavily modified version of XMPP. Additionally they use Firebase to get push notifications.

I suggest you to research how other XMPP apps handle push notifications (Xabber, Yaxim, Kontalk use Smack).

I also use firebase for getting notifications, and if the app is not running, I start those service back and fetch the chat messages and show it to the user.

smack logs tell that group message has been sent, but i never receive them on the other members

<message to='muclight15253578100@muclight.localhost' id='Auy8P-57' type='groupchat'> <body>hiii</body><active xmlns='http://jabber.org/protocol/chatstates'></active><request xmlns='urn:xmpp:receipts'></request></message>
05-04 05:14:21.207 4319-4359/com.techsbiz.apnaapp D/SMACK: SENT (0): 
<r xmlns='urn:xmpp:sm:3'/>

on the other end i recv a packet with content something like this

RECV (0): <iq from='916666655555@localhost' to='916666655555@localhost/android' id='pushBDDA8AA4BD0C9449' type='set'><query xmlns='jabber:iq:roster'><item ask='subscribe' subscription='none' jid='null@localhost'/></query></iq>
RECV (0): <r xmlns='urn:xmpp:sm:3'/>
SENT (0): <a xmlns='urn:xmpp:sm:3' h='23'/><iq to='916666655555@localhost' id='pushBDDA8AA4BD0C9449' type='result'></iq><r xmlns='urn:xmpp:sm:3'/>
RECV (0): <a xmlns='urn:xmpp:sm:3' h='31'/>

and this happens only when, both the chat activities are open, if the group list is visible, I am able to get the messages but not in the case, where group chat UI(from where a users sends in a message ) is open i fail to recv any messages

Sounds like you made a mistake in your implementation then. I’d assume that some listener is registered in one scenario but not in the other, but I can only speculate.

but in that case i am not even receiving any packet on any listener.

But what does these packets mean?

RECV (0): <iq from='916666655555@localhost' to='916666655555@localhost/android' id='pushBDDA8AA4BD0C9449' type='set'><query xmlns='jabber:iq:roster'><item ask='subscribe' subscription='none' jid='null@localhost'/></query></iq>
RECV (0): <r xmlns='urn:xmpp:sm:3'/>
SENT (0): <a xmlns='urn:xmpp:sm:3' h='23'/><iq to='916666655555@localhost' id='pushBDDA8AA4BD0C9449' type='result'></iq><r xmlns='urn:xmpp:sm:3'/>
RECV (0): <a xmlns='urn:xmpp:sm:3' h='31'/>

The <r/> and <a/> packets are responsible for stream management.
The <iq/> packets are probably a received unsubscription request and the acknowledgement.

Please take a closer look at RFC6120 and XEP-198 for more information.