chatCreated event fired for every incoming message

Hi, I have the code below, which adds a chat listener when there is a new chat session created

by a remote ID. However, I find that the chatCreated event is fired for every incoming message

I receive, if the conversation is initiated by the other party. (It only gets fired once if I initiated the

conversation.) This does not sound very efficient because for every incoming message a new Chat

object and a new MessageListener object are created. Am I doing something wrong here? Is there

anyway to reuse an existing Chat and MessageListener object in this case? Thanks!

ChatManager chatmanager = connection.getChatManager();
chatmanager.addChatListener(new ChatManagerListener() {
public void chatCreated(Chat chat, boolean createdLocally) {

System.out.println("chatCreated " + chat.getParticipant() + " " + chat.getListeners());
if (!createdLocally) {
chat.addMessageListener(new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: " + message.getBody());
}
});
}
}
});

The MessageListener that you have implemented is not specific to the chat session. Therefore, it will receive every Message received. You need to add a filter to the MessageListener.

Look into a MessageTypeFilter

This is really due to a bug in 3.1.0

SeeSmack 3.1.0 creates a new chat for every incoming message

Hmm. Not sure what you meant. Did you mean I should create MessageListener only once, and if there si a MessageListener, skip this step?

No you shouldn’t.

Just apply the patch I’ve linked to the smack source, build it, and it would work as you originally expected it to.

Yes, your patch seems to have fixed the problem. Thanks!