Vinod Patel
Vinod Patel

Reputation: 430

Multi Device support in XMPP Smack Chat Application

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.

Upvotes: 2

Views: 1271

Answers (3)

B378
B378

Reputation: 1117

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

Upvotes: 0

Robin
Robin

Reputation: 24282

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.

Upvotes: 0

Mickaël Rémond
Mickaël Rémond

Reputation: 9055

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>

Upvotes: 1

Related Questions