How do I synchronize sent/received Chat messages?

Hi,

I am a newbie, developing a basic IM application using the XMPP library.

All the users etc. have been setup using Spark.

For development, I login from my program using “user1” and try to send/receive messages with the user’'s contacts who are on Spark.

From my program I can send messages easily using Chat:

Chat chat = new Chat(connection, “user2@sdd”);
chat.sendMessage(“Hey how are you?”);
System.out.println(chat.getThreadID());

This opens up a new chat window in “user2”’'s Spark client and displays the sent message.

Now, I want to detect incoming messages.

So, based on the documentation and several posts, I wrote the following:

PacketFilter filter = new PacketTypeFilter(Message.class);
PacketListener myListener = new PacketListener()
{
public void processPacket(Packet packet)
{
Message message = (Message)packet;
Message.Type messageType = message.getType();
if (messageType == Message.Type.CHAT)
System.out.println(message.getFrom() + ", " + message.getBody());
else
System.out.println(“Not a chat message”);
}
}};
connection.addPacketListener(myListener, filter);

Now when user2 replies to user1, the incoming message is correctly caught by the listener, and the message gets displayed.

However how do I know who this incoming message belongs to?

There could be several chats runnign simultaneously, and ultimately the received message has to be displayed in a gui window.

Both message.getThread() and chat.getThreadID() return different values.

How do I synchronize sent/received messages?

Another thing:

Where is the following code useful? It never gets executed:

chat.addMessageListener(new PacketListener()
{
public void processPacket(Packet packet)
{
System.out.println(“1 got message in Chat”);
}
});

Please help – been stuck on this.

Thanks,

Karan

Well you know who the message comes from because

packet.getFrom()

will give you the jid of the contact who sent the message.

Smack currently does not have a way of managing incoming chats (I think this may be on the list of things to do) so the best option currently is to store your chats in some kind of datastructure (a hashmap works quite well, you can use the jid as the key) and when you receive a message just check to see if you have a chat currently in the map.

Jon,

Correct me if I’'m wrong – I am very new to Smack and Jabber.

The JID of who its from is not going to solve the problem.

If I am in a chat with a user A, and I’'m also in a group chat with users A and B, then when I get a message from user A, where does it go: to the private chat, or to the group chat?

So, I need to somehow manage the ThreadID of the chat.

And once again, the problem comes up: when I initiate the chat, the chat.ThreadID will be different from the message.ThreadID that I get in reply.

Did you face something like this in your work? How do you implement this nicely?

Smack currently does not have a way of managing incoming chats (I think this may be on the list of things to do)

Does anyone know if this is in the works? I didn’'t notice this in the IssueTracker.

Thanks,

Karan

This post is helpful. Thanks Jon.

I now have a better understanding of what you were trying to say.

http://www.jivesoftware.org/community/thread.jspa?threadID=19744

Even if the users are in a Chat as well as a GroupChat the PacketListener can segregate the incoming message by using a simple switch-case

in the processPacket to detect for Message.Type.CHAT and Message.Type.GROUP_CHAT, and then take appropriate action.

So, by simply maintaining a Map of the User IDs, we know how to route the message.

In either case the jid is different for a Group Chat anyhow.

Off hand I think its something like username@groupchatname

So you would always be able to tell the difference between them anyhow.

Along with this, you can add a MessageListener to your Groupchat to specifically listen out for new message in it.

In either case the jid is different for a Group Chat anyhow.

Thanks for that info

Along with this, you can add a MessageListener to your Groupchat to specifically listen out for new message in it.

How??

I’'ve mentioned this at the bottom of the first post.

What has to happen to fire the Listener below??

chat.addMessageListener(new PacketListener()
{
public void processPacket(Packet packet)
{
System.out.println(“1 got message in Chat”);
}
});

I know the above code is for a Chat and not a GroupChat – but still, the code above never gets executed, even if the Connection.PacketListener is detecting incoming packets.

Well what you can do is hook up a packetlistener which listens out for both packets of type message and who are from your contact.

So

// pseudo code
     // adding a packetlistener to your connection to listen out for packet      PacketTypeFilter filter = new PacketTypeFilter(Message.class);
          connection.addPacketListener(new MessagePacket, filter);

And in your MessageListener

processPacket(Packet packet) {               // if not chatting create new chat window
              new ChatWindow(message.getFrom);
       }

Then in your ChatWindow

public ChatWindow(String jid) {
                AndFilter filter = new AndFilter(new PacketTypeFilter(Message.class), new FromContainsFilter(jid));
          conn.addPacketListener(this, filter)
      }       // Only Message packets from your contact will get delivered here
      processPacket(Packet packet) {             // display message in window
      }

hth

Jon