2

I am developing Android Chat Application.

My requirement is to deliver messages to multiple devices.

Consider this scenario between two users User A and User B:

User A logs in and sends message to User B.
User B logs in from Device 1 and receives message from User A.
User B logs in from Device 2 but does not get message from User A.

According to my requirement User B should get message from User A in Device 2 as well.

How can I achieve this?

Thanks.

Vinod Patel
  • 430
  • 1
  • 7
  • 19

3 Answers3

1

In XMPP, the message are only delivered in a single go either:

  • to the online resources sharing the highest priority,
  • to the first client that connect through offline message delivery.

However, if you want other clients to resync, you should rely on XEP-0313: Message Archive Management. This specifications describe how a client can access a message history and resync its state.

You can for example query the message archive for all messages after a given time. This will allow the client that connect to get all the messages it missed since it was last online:

<iq type='set' id='juliet1'>
  <query xmlns='urn:xmpp:mam:0'>
    <x xmlns='jabber:x:data'>
      <field var='FORM_TYPE'>
        <value>urn:xmpp:mam:0</value>
      </field>
      <field var='start'>
        <value>2010-08-07T00:00:00Z</value>
      </field>
    </x>
  </query>
</iq>
Mickaël Rémond
  • 9,035
  • 1
  • 24
  • 44
  • It will be really helpful if you can you give me some examples. Thanks – Vinod Patel Jul 20 '15 at 13:45
  • Some examples of what ? The XEP already includes example stanza regarding how to enable query the Message archive. You can for example by time: http://xmpp.org/extensions/xep-0313.html#filter-time Not sure what more I could provide, as you can clearly asked for all messages since the last one you received. Editing the message to be even more explicit on how to resync messages on device connection. – Mickaël Rémond Jul 20 '15 at 13:49
  • i am using Smack API library for communicating with ejabberd SaaS, but I am not able to find how to get message archive through Smack. The link you mentioned only explains protocol. – Vinod Patel Jul 20 '15 at 13:55
  • 1
    I never used Smack, but if you are serious about XMPP development, you need to know how this happens at the protocol level. There is no way around it in my humble opinion. – Mickaël Rémond Jul 20 '15 at 14:00
0

In addition to Mickael's answer, you should be aware of a few things. If you specify a full JID as your to address, then the message will only be delivered to that single endpoint.

Message deliver to multiple resources for the same user will only occur if

  • The to address is the bare JID
  • All connections have the same priority
  • The server is configured to deliver to multiple endpoints.

That last point is crucial. According to the specification, the server can handle messages sent to the bare JID in 2 ways.

  • Send to one of the connections with the highest priority. Which one is determined by the server, it could be the first one connected, the last one, or a random choice if there are multiple connections with the same priority.
  • Send to all of the connections with the highest priority.

So, unless you know your server supports and is configured to allow the second choice, you can't accomplish what you are trying to do anyway.

Robin
  • 24,062
  • 5
  • 49
  • 58
0

In order to participate in the instant messaging and presence activities, a client (i.e device) should establish a session on the server.

As given in XMPP Documentation

If there is already an active resource of the same name, the server MUST either (1) terminate the active resource and allow the newly-requested session, or (2) disallow the newly-requested session and maintain the active resource. Which of these the server does is up to the implementation, although it is RECOMMENDED to implement case #1. In case #1, the server SHOULD send a stream error to the active resource, terminate the XML stream and underlying TCP connection for the active resource, and return a IQ stanza of type "result" (indicating success) to the newly-requested session. In case #2, the server SHOULD send a stanza error to the newly-requested session but maintain the XML stream for that connection so that the newly-requested session has an opportunity to negotiate a non-conflicting resource identifier before sending another request for session establishment.

(https://www.rfc-editor.org/rfc/rfc6120#section-7.7.2.2)

Therefore you should first decide the way you are going to handle the sessions, according to the app requirement. Now since you are using Ejabberd you can configure it by defining the option resource_conflict

However, if you still want to use multiple sessions you can use Message Archive Management - XEP-0313

So you can store chat history on the server and then retrieve. This can be configured in ejabberd by using the option mod_mam

Community
  • 1
  • 1
B378
  • 987
  • 2
  • 12
  • 27