How to get the latest messages posted in a GroupChat?

Hi,

I have noticed that my Smack client receives the latest posts from Jive messenger when it joins a GroupChat. So, I was wondering if it exists a way to obtain these latest posts after having joined a GroupChat, a kind of GroupChat#getLatestMessages() method (the number of returned messages may depends of the Server configuration). Does someone can help me?

Thanks,

Seb

Seb,

Don’'t know if you are using GroupChat or MultiUserChat. In any (according to javadoc), MultiUserChat is the one to use. Once you have joined a room, you can add a packet listener like below, the listener will get the chat history. The acutal number of messages you receive is determined by the history setting on jive messenger. See GroupChat page, history menu item.

MultiUserChat room = new MultiUserChat(server, chatRoomJID);

room.join(vickname);

room.addMessageListener(new PacketListener()

{

private int packetCount = 0;

public void processPacket(Packet packet)

{

packetCount++;

System.out.println(“Received packet:” + packetCount);

System.out.println(packet.toXML());

}

});

Conor

Thanks Connor, but I think a PacketListener cannot do the job (if I understood, it only gets incoming packet and I have to store these packets to get the history I need).

Indeed, the client I am developping is little bit “strange” since it is not a real time application. What I mean is that I do not need to get messages from a room immediatly after they are posted on the server, but I need to be able to get this messages to respond to a user request. As it is an SMS chat application, you can easily understand that users do not want to get room’'s messages on a real time basis but only when they ask for it. That is the reason why I am looking for a getLatestMessages() method to avoid storing the messages on my client application side whereas they are stored on Jive Messenger.

Does someone see what I mean?

Seb,

Ok I see your problem. I think the following may work for you. When you join the room, you can specify the DiscussionHistory, this object contains a “since” value, indicating that you are only interested in messages after/since a particular time.

This time can be when the user last checked. You could store this time on your client, or better still, store it on the server as a user property! This means no matter where you login from you will always get only the new messages since you last checked.

Hope this help!

Conor.

Thanks a lot Connor, I have just had a look to DiscussionHistory mechanisms and, if I have understood it well, I would only get messages (history) when I join the MUC.

Of course, I can store these messages to be able to return them for user when he wants, but that is not very satisfying considering the SMS/Wap environment.

Indeed, after joining a MUC or GroupChat, I would like to be able to send messages (that is possible with sendMessage(String)) and get X (for example 3) latest messages posted in MUC or GroupChat at particular time without a Listener (I do not know if it is possible).

As I did not find it with Smack, I was wondering how to do this (if it could be implemented with XMPP). So, is there another way to solve my problem that could suits me more than Connor’'s solution (which can do the job but which is not perfect)?

Hey Seb,

As you have seen, MultiUserChat does not treat the Messages that where received as part of the room’‘s history in any special way. That means that you will need to keep the room’'s history in your own class.

I guess that you must be using another class on top of MultiUserChat that sends the messages to the SMS user. IMO, you may keep the history in your class (or any other class of your layer) and send the history to the user whenever it is needed. FYI, Messages that were received as part of the room’'s history will contain a PacketExtension with namespace ‘‘jabber:x:delay’’.

There is no need to use a PacketListener. You can use #nextMessage() or #nextMessage(long) in order to get the room messages. Since messages that belong to the room’‘s history are not a special case, those methods will return the messages received as part of the room’'s history as well as any regular message.

Regards,

– Gato

Gato >> Considering your explanation, I guess that the messages sent by Jive Messenger when someone joins a room is a special function of Jive Messenger that does not represent a defined XMPP mechanism, isn’'t it?

As a matter of fact, I am going to think about the best way to store room’'s messages…

Thanks for your help in my XMPP Quest (and be sure, I’'ll be back!)

Hey Seb,

The room history is something standard defined in the MUC JEP. You can follow this link to learn more about it: http://www.jabber.org/jeps/jep-0045.html#enter-history.

We are here. So let us know if you need further help.

Take care,

– Gato

Gato >> Ok, in that case, would it be possible to code a new method to MUC that would request to the server the sending of the current room history, even if the user isJoined()?

In fact, this could be possible if the server sends the room history responding to a special request and not, as I fear, responding to the join request…

Hey Seb,

The MUC service will send the room history just for joining the room. So there is no special request for getting it. This is why you may need to store it for later use as it seems to be what you need.

Let me know if you need any help.

Regards,

– Gato

Gato >> muchas gracias

I think I have all the information I need to sort out my history issue. So I am going to continue to developp my product… and I will certainly come back soon with another insurmountable problem (things change, other not!)

Regards,

Seb