Chat threadId issue

Hey, I’m having an issue using smack connected to a gmail account, that after a while, outgoing messages are not received by my contact anymore.

So I started digging into the Chat threadId.

I was under the impression that,

Chat chat = manager.createChat("alscuy@gmail.com", listener);

String firstId = chat.getThreadID();

chat = manager.createChat("alscuy@gmail.com", listener);

String secondId = chat.getThreadID();

System.out.println(firstId);

System.out.println(secondId);

should return the same id?

But it’s not.

I don’t know if it’s related to this http://issues.igniterealtime.org/browse/SMACK-269

which should be fixed in 3.2.1 (the one I’m using).

Can someone please confirm this behaviour?

Thanks in advance!

You have created a new chat, each chat has a unique thread ID. This is the expected behaviour, as it allows for multiple unique chats to the same JID. Otherwise there is no way to distinguish messages between chat 1 and chat 2.

The issue you referenced deals with matching incoming chat messages that do not contain a thread id, as some clients don’t use them.

Thanks for your response rcollier. So what would be the right way to do this. Should I store a map between jid and Chat in order to reuse already created chats and avoid creating a new one for each message? Or is it just fine to create a new one for each message?

You would be expected to maintain and use the same Chat instance for the duration of your chat. Once created, the Chat is maintained in the ChatManager for the duration of your program. Incoming messages will be matched to the existing Chat and distributed to its listeners.

Creating a new Chat for each message will accumulate messages within the ChatManager, and produce unique chats (via threadId) to your user. If the end client uses the threadId and thus replies with it, the message will be sent to the Chat instance that matches.

Have you read the documentation with regards to Chat?