2

When device1 is sending the message to the conference room "del@conference.jabber.org" the message is dispalyed in the chat list as well as a duplicated message is also displayed that is being send by the conference room "del@conference.jabber.org". I'm stuck, why i'm getting duplicate message.

public void setConnection(XMPPConnection connection) {
    this.connection = connection;
    if (connection != null) {
      PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
      connection.addPacketListener(new PacketListener() {
        @Override
        public void processPacket(Packet packet) {
          Message message = (Message) packet;
          if (message.getBody() != null) {
            String fromName = StringUtils.parseBareAddress(message.getFrom());
            String[] parts = fromName.split("@");
            String from = parts[0].trim();
            messages.add(from + ":");
            messages.add(message.getBody());
            // Add the incoming message to the list view
            mHandler.post(new Runnable() {
              public void run() {
                setListAdapter();
              }
            });
          }
        }
      }, filter);
    }
  }

The send message is on button click, which is as follows

Button send = (Button) this.findViewById(R.id.sendBtn);
            send.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Message mg = muc.createMessage();
                    String text = textMessage.getText().toString();       
                    mg.setBody(text);
                    Log.i("XMPPChatDemoActivity ", "Sending text ");
                    if (connection != null) {
                        connection.sendPacket(mg);
                        messages.add("Me :");
                        messages.add(text);
                        setListAdapter();
                    }
                    textMessage.setText("");
                }
            });

and this is what i have written to connect the conference room

muc = new MultiUserChat(connection, "del@conference.jabber.org");
            muc.join("alias name","password");

output what i'm getting when sending message

me: hello
del: hello

what i want i no duplicate message when i send the message i.e

me: hello
Flow
  • 23,572
  • 15
  • 99
  • 156
Stolen Skull
  • 25
  • 1
  • 7

2 Answers2

4

When you're in a MUC room you receive copies of all the messages, including your own. http://xmpp.org/extensions/xep-0045.html#message - "and reflect the message out to the full JID of each occupant."

So for MUCs (not for direct messages) you will get a duplicate if you log both on send and on receive (assuming you have sufficient access to post, etc.). Your options are, largely, either to not log it on send (which is the option most clients go for) or to attempt to do smart message matching to detect when you receive your own message and elide it. The former option ensures that everyone sees a consistent view of message ordering, which some people find very useful.

Kev
  • 2,234
  • 1
  • 12
  • 6
  • Thanks kev, i read some documents on MUC and knew that it will broadcast the message in the conference room. I solved my problem by message matching. – Stolen Skull Aug 28 '14 at 03:36
2

Maybe your chat server sent your message to you also?

So you add one message manually in onClickListener and then the same message received from server.

I think, it will be right not to add messages from onClickListener - add only those that server sends.

Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60
  • I'm pretty sure that the MUC is reflecting the message. I leave it up as exercise to the OP to read about that in XEP-45 and correct me if I'm mistaken. – Flow Aug 27 '14 at 11:02