Export to GitHub

asmack - issue #3

Can only receive chats by polling


Posted on Mar 2, 2010 by Happy Giraffe

I can't get the smack example code to work right. Sending messages works fine, but processMessage() never fires when a message comes back. But if I try the polling code from http://today.java.net/article/2006/10/04/instant- messaging-java-made-easy-smack-api I can receive messages just fine.

That is, the code below only sends "testing". But if I uncomment the big block at the bottom, I receive messages as expected.

I'm running all this on an Android 2.1 AVD.

    // Sorry, I'm not good with Java.
    new Thread(new Runnable() {
      public void run() {            
        XMPPConnection xmpp = new XMPPConnection("jabber.iitsp.com");
        try {
          xmpp.connect();
          xmpp.login("actualusername","password");
        } catch (XMPPException e) {
          Log.v(TAG, "Failed to connect to " + xmpp.getHost());
          e.printStackTrace();
        }
        ChatManager chatmanager = xmpp.getChatManager();
        Chat newChat = chatmanager.createChat("foo@example.com", new 

MessageListener() { public void processMessage(Chat chat, Message message) { try { Log.v(TAG, "Got:" + message.getBody()); chat.sendMessage(message.getBody()); } catch (XMPPException e) { Log.v(TAG, "Couldn't respond:" + e); } Log.v(TAG, message.toString()); } });

        try {
          newChat.sendMessage("testing");
        } catch (XMPPException e) {
          Log.v(TAG, "couldn't send:" + e.toString());
        }

        // IF I UNCOMMENT THE FOLLOWING BLOCK, I CAN RECEIVE
        //  MESSAGES AS EXPECTED
        /*
        PacketFilter filter 
            = new AndFilter(new PacketTypeFilter(Message.class), 
                            new FromContainsFilter("foo@example.com"));

        PacketCollector collector = xmpp.createPacketCollector(filter);

        while(true) {
            Packet packet = collector.nextResult();

            if (packet instanceof Message) {
                Message msg = (Message) packet;
                Log.v(TAG, "Got message:" + msg.getBody());
            }
        }
        */
      }
    }).start();

Comment #1

Posted on Mar 2, 2010 by Happy Giraffe

Incidentally, I just figured out how to connect to google accounts:

Use XMPPConnection("gmail.com") and login("username@gmail.com", "password")

Comment #2

Posted on Mar 2, 2010 by Grumpy Rhino

Could you add XMPPConnection.DEBUG=true; and Connection.DEBUG=true; before stating the Thread? Can you provide a log of the messages send (minus the sasl part, of course).

What does "you can receive messages" mean? Can you receive messages in your loop and in your chat manager or just in your loop?

TIA, René

Comment #3

Posted on Mar 2, 2010 by Helpful Elephant

I think I know this bug. You also need to listen for new Chats not only for Messages in your existing chat since the ChatManager class in the original Smack has a bug which creates for every incoming message a new Chat. This happens if the incoming Message hasn't got a threadid or another threadid than the message you sent. Your PacketCollector instead listens for all Messages it gets from the other JID no matter what threadid they have. A PacketListener on the connection which listens for Message packets should also work.

Comment #4

Posted on Mar 2, 2010 by Grumpy Rhino

THX till, http://www.igniterealtime.org/issues/browse/SMACK-269 is what you mean, right? Sounds like I should cherry-pick that one, too.

Any objectives?

Comment #5

Posted on Mar 4, 2010 by Helpful Elephant

Yeah, thats what I meant. At the moment I completely circumvent the ChatManager and map the chats according to the jid. I'm completey ignoring the threadid since most client don't set the threadid. This works pretty fine for me.

Comment #6

Posted on Mar 5, 2010 by Grumpy Rhino

Ok, I'll have to read the spec, depending on that I'd - clone the chat manager and provide a xmpp-thread-unsafe chat manager (if smack is right) - apply the patch (if smack is wrong)

Comment #7

Posted on Mar 13, 2010 by Grumpy Rhino

http://xmpp.org/rfcs/rfc3921.html#rfc.section.2.1.2 looks like you're right. Clients MAY send a thread-id for messages, but they aren't enforced to do so.

2.1.2.3 is even worse. Looks like messages should be grouped by jid,resource,thread (with NULL being a correct value for grouping). Starting a thread with a thread id is bad because we can't be sure if a reply (if any) will contain the thread id.

Google code and any RFC viewer should implement dislike button....

Comment #8

Posted on Apr 6, 2010 by Swift Wombat

I see there's a new build of the code. Has this issue been addressed in that build?
Thx!

Comment #9

Posted on Apr 8, 2010 by Swift Wombat

Just confirmed it's fixed with asmack-2010.04.02.jar. Thanks!

Comment #10

Posted on Apr 8, 2010 by Grumpy Rhino

Oh nice, didn't see the patch that fixed it, but glad to see your problem has been addressed! Thanks for rechecking, and happy hacking!

Comment #11

Posted on Apr 22, 2010 by Happy Ox

Comment deleted

Status: Done

Labels:
Type-Defect Priority-Medium