Resources and Priority

In the previous section, we saw how the resource is used to “qualify” certain queries to a servername, to hold information such as version numbers, and to represent users in a conference room. However, the resource is traditionally seen as a way of making a distinction between simultaneous connections by a user to the same Jabber server. For example, if you connect to a Jabber server using the same username and password on three different machines (or "resources"), the Jabber server will look at the resource part of the JID to determine which client to route messages to.

For the purpose of this example, let's say that the three resources are a workstation, laptop, and a PDA. Each client is connected to the same Jabber server, so the resource part of the JID can be used to distinguish between the three connections. They could also be used to differentiate between the three connections coming from the same client host.

The classic explanation serves us well here: In a work situation, I might be connected to my Jabber server using a client on my workstation. I might also be connected, with the same username, to my Jabber server on my laptop that's sitting next to my workstation. Furthermore, I might have a handheld device that runs a small Jabber client, that I'm connected with too.

On each client machine, I'm connecting using the same credentials (username and password) to the same Jabber server. So the resource part of a JID can be used to distinguish between my three connections. The three 'resources' are my workstation, laptop and handheld, in this example. In another example, they might be used to differentiate between three different connections coming from the same client host (say, my desktop).

The resource part of a JID allows a user to be connected to Jabber (specifically the JSM, which manages users and sees user sessions as separate entities) multiple times.

"But", I hear you say, "what happens when someone sends you a message? To which client is the message sent?"

This is where the concept of connection priority comes to our aid. Each Jabber client connection can be given a priority. When a user has more than one concurrent connection to a Jabber server, the priority is used to determine to which connection any messages intended for that user should be sent. [1] The connection with the highest priority value is the connection to which the messages are sent. [2]

Figure 5-1 shows priority in action. In this example, Sabine's message is sent to the Jabber client on the Desktop, as it has a higher priority. Note that with Jabber priority, 1 has a lower priority than, say, 5. The higher the number, the higher the priority.

Figure 5-1. Resources, Priority, and Message Delivery


   Sender                                           Recipient
   ------                                           ---------
  sabine@yak                                        dj@yak
                     +---------------+
  +--------+         |               |         +------------------+
  |        |         |    Jabber     |         | Client 1         |
  | Client |---------|    Server     |---------| Resource: Laptop |
  |        |         |               |         | Priority: 1      |
  +--------+         |      yak      |         +------------------+
                     |               |           dj@jak/Laptop
                     |               |
   Message ------------------+       |         +------------------+
   to dj@yak         |       |       |         | Client 2         |
                     |       |       |---------| Resource: Desktop|
                     |       +---------------> | Priority: 2      |
                     |               |         +------------------+
                     +---------------+           dj@yak/Desktop

If there is a priority tie, for example if I have connected to my Jabber server with a client on my laptop (where I specified the resource “Laptop”) and also with a client on my desktop (where I specified the resource “Desktop”), and the settings in both clients have their priorities set to the value 1, the most recent connection to be made wins and receives the messages.

It is also possible to direct messages to a particular client. Taking the example from Figure 5-1, if sabine@yak were to specify dj@yak/Laptop instead of dj@yak as the recipient for a message, her message would go to Client 1 (Laptop), not Client 2 (Desktop), despite Client 1's lower priority value.

Priorities are specified when a user sends presence information. We will see this later in the section called The Jabber Building Blocks. It makes sense for the priority to be associated with a user's presence, rather than a user's client connection. For example, if the priority was specified at connection time, the user would have to disconnect and reconnect if she wanted to change priority. As it stands, she just has to send presence information containing a new priority value to change it. Figure 5-2 shows a WinJab client popup window used to change presence information. The value of the current priority can be changed.

Figure 5-2. Changing presence and priority in the WinJab client

Notes

[1]

“Messages” is used in the generic sense; actually any packet destined to the user is implied. See the section called The Jabber Building Blocks in this Chapter for information on the types of packets that are send and received.

[2]

Priority values must be a positive integer, and cannot be zero or less.