Help with the packetListener tutorial

Hello all,

I got the following from the tutorial

// Create a packet filter to listen for new messages from a particular

// user. We use an AndFilter to combine two other filters.

PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class),

    new FromContainsFilter(""));

// Assume we’'ve created an XMPPConnection name “connection”.

// Next, create a packet listener. We use an anonymous inner class for brevity.

PacketListener myListener = new PacketListener() {

public void processPacket(Packet packet) {

// Do something with the incoming packet here.



// Register the listener.

connection.addPacketListener(myListener, filter);


Currently within the processPacket method, I write to a file.

I am doing this because it is a web based app.

Instead, I need to return a Message object from the processPacket method but am not sure how to do it(perhaps it is illegal?)

I tried:


but received error.

Any ideas as to how I can return the Message object from the listener?


You cannot change the returning type of an implemented method, so it’'s probably “illegal”.

I suggest you define an instance object representing the file in your implementation class

and simply write something to it in the processPacket method.

It would be helpful if you can explain your situation in a further detail.

Who are you returning the Message to? Keep in mind that your registered your listener with the Smack framework, and it is not expecting a Message in return. This is largely why you cannot change the method to return a Message.

If you are referring to returning a message to the sender of the received msg, then you are asking the same question I am in a different thread. I haven’'t found any methods to get from either a Packet or a Message to the Chat in order to createMessage() and sendMessage() back to the sender.

My quess at this time is that I might have to setup a ConnectionListener to be called when the Chat is established and track it myself perhaps in a Map keyed on the “From” value. This does seem like a method that is missing in the Smack API. Obviously if I am receiving a message, that message originated from a Chat which I might want to respond to.

I’'m open to some help here as well.


AFAIK smack currently does not have such a mapping as from a Message object to a Chat Object. I seem to remember reading a thread a while ago that it maybe something that will be added in the future.

As for the moment, most implementations I’'ve seen do as you suggest. Storing chats in a construct such as a Map, and using the from value as a key.


Hey Guys,

FYI: we are working on some major changes to how chats are handled for the next version of Smack, this goes without saying that this contains major API changes. Here is what we are thinking of:

Central chat control through a ChatManager class through this you have the ability to add a MessageListener when fired the MessageListener looks like this:

void messageRecieved(Chat chat, Message message);

Finer grained control will be retained by keeping the ability to add a MessageListener to a particular chat.

Let me know what you guys think.



I did get this working, and it is a lot simpler than I thought. No Map of Chats or anything like that. The trick is not to think about the Chat object as a connection as I was. It is a sequence of messages. So my packet listener gets a Message (filtered, so I know it is a message), and I do the following:

Message msg = (Message)packet;

Chat chat = new Chat(connection, msg.getFrom(), msg.getThread());

Message reply = chat.createMessage();


That did it. As far as the new ChatManager, I’‘m not sure. I don’‘t want to lose the Chat.nextMessage() capability as I use it for synchronous request/reply exchanges. Sounds like it would work for asynchronous exchanges or situations where I want to handle messages through one listener, though. I’‘m OK with the current PacketListeners for that at the moment, as I don’'t break out separate Listeners for each chat. It might be handy though.