Stolen Skull
Stolen Skull

Reputation: 25

Duplicate message received in XMPP Multi User Conference Room

When device1 is sending the message to the conference room "[email protected]" 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 "[email protected]". 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, "[email protected]");
            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

Upvotes: 2

Views: 2311

Answers (2)

Kev
Kev

Reputation: 2264

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.

Upvotes: 4

Artem Mostyaev
Artem Mostyaev

Reputation: 3908

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.

Upvotes: 2

Related Questions