Messages being lost with MessageListener but not with PacketListener?

We are trying to implement a one to one chat between two users. We originally used a MessageListener but found that a number of the return messages were not being delivered to the person initiating the chat. We then changed our implementation to use a packetlistener. Now we receive the same message back twice, presumably picked up once by the messagelistener and once by the packet listener.

public void sendMessageToUser(PrivateChat privateChat, String chatMsg) {
String jid = privateChat.getUserName()+"@" + host;
connection.addPacketListener((PacketListener) privateChat.getListener(), privateChat.getPacketFilter());
Chat chat = connection.getChatManager().createChat(jid, privateChat.getListener());
try {
chat.sendMessage(chatMsg);

    } catch (XMPPException e) {
        throw new IllegalStateException("Unable to Send Private Message");
    }
}

PrivateChat’s getListener returns our own listener which implements both messagelistener and packetlistener (it has both a processPacket and a processMessage).

All the examples on the API of private chats use messagelistener and not the packetlistener, could anyone advise what the differences are and which we should be using? Is there any reason a messagelistener would lose some of the return messages?

I’m having the same issue. My message listener fails to hear anything, even though the debugger shows data coming in. I added a packet listener and it works.

The only diff I see is that the message listener is attached to the chat, and the packet listener is attached to the connection. It is almost as if the chat manager is loosing the chat or I’m loosing the manager.

P.

I concur with what has been stated. I have given up on MessageListener and went with Filtered Packet Listeners. If anyone else knows of a better way let me know.

With a private chat, you would probably want to use a ChatManager and add a chat listener instead of the Message or Packet Listeners.

Let me know if this helps

Thanks Dave, we are using a ChatManager but not implementing ChatListener on the custom listener we use. I can’t actually see a ChatListener on the most recent Smack API, was it the ChatStateListener?

All the examples on the net seem to show a MessageListener being used, yet a few people here have said they are losing messages. Is this a bug?

Thanks

I figured out what was happening in my case.

I am using XMPP as the communication protocol between multiple software components. In testing, I was using the Spark Client to bounce messages to. As it turned out, the Spark client was responding to the chat session using a different thread, then was used to create the chat originaly. When I created a client from scratch, and used the thread passed to it, the message listener worked.

For what I’m doing, I have decided that the Message listener isn’t the best way to go, regardless of my challange. I don’t realy care about a specific chat session, I need to look at any packet that arrives, hence the packet listener is the way to go.

P.

There is a ChatManagerListener this will allow both ends to recognize when a chat session has been created. This will allow a synch between two seperate clients on to one chat thread.

ChatManager.addListener(ChatManagerListener)

I believe chatcreated will be the method that you would implement.

I have typically used a PacketListener for most of my Messages. I just use a filter to identify which packets I want to listen for.

If you have a difficult time determining a filter type that you might need let me know. I like the AndFilter for messages that have more than one item to filter.

Great, I’ll give that one a go and see how I get on. I guess it makes sense that the different thread id is causing the issue. Maybe this should be one for a common FAQ?

I am not real sure about the Common FAQ? Could be a good thing for all to know. I am on here to contribute how I can, as I have posted questions as well. Although, none of my questions have actually been answered. ;(

Hey fellas,

what are you guys doing with the messages you get back. I can get them them displayed on the console real time but if i try setting the incoming message to any varaible it always returns null.

Chat chat = connection.getChatManager().createChat("kryptonomicon@jabber.org", new MessageListener() {

    public  void processMessage(Chat chat, Message message) {

        //  Print out any messages we get back to standard out.

       

         //  this works and outputs messages as they come in

         System.out.println("Received message: " +  message.getBody());

        //  this does nothing.                   

        setReceivedMessage(message.getBody());

         System.out.println(getReceivedMessage);                                                      

     }

 };

Am I using this incorrectly, I can only get the incoming messages to be printed out to the console - need to be able to view them on html.

Any assistance would be greatly appreciated. Using Smack api 3.1.0 / Tapestry 5 / Spring / Hibernate.

…kace

getBody simply returns a String (no doubt you know that).

Without seeing more of your code, the most I can say, it you should be debugging your bean set and get methods.

P.

Here is further code:

private XMPPConnection connection; @Persist
private String receivedmsg; void onActivate() {
     if (connection == null || !connection.isConnected()) {
                        //XMPPConnection.DEBUG_ENABLED = true;
            connection = new XMPPConnection("jabber.org");
            try {
                connection.connect();                  connection.addPacketListener(smak.myPacketListener, filter);
                SASLAuthentication.supportSASLMechanism("PLAIN", 0);                  connection.login("kryptonomicon", "blahblah");
            } catch (XMPPException e) {
                e.printStackTrace();
            }
      }
} @OnEvent(component = Constants.FORM_ID, value = EventConstants.PREPARE)
  protected void prepareForm2() {
        if (chat == null) {
                        chat = dashbaord.getConnection().getChatManager().createChat("kryptonomicon@jabber.org", null);
            try {
                chat.sendMessage("This is from krypts server");
            } catch (XMPPException e) {
                e.printStackTrace();
            }
        }     } private PacketTypeFilter filter = new PacketTypeFilter(Message.class); private PacketListener myPacketListener = new PacketListener() {
        public void processPacket(Packet packet) {             // do something with your packet here             Message message = (Message) packet;
            // this works
            System.out.println(message.getBody() + " from processpacket");
            String body = message.getBody();
            // this works
            System.out.println("Message from: "  + " " + body);             // nothing happens here - getReceivedmsg() is never called on debug
            setReceivedmsg(body);
            System.out.println(getReceivedmsg());                     }
    };     public String getReceivedmsg() {
        return receivedmsg;
    }     public void setReceivedmsg(String receivedmsg) {
        this.receivedmsg = receivedmsg;
    }

does this help at all? How are you guys getting the message out to other parts of your application?

Thanks

…kace