• No results found

Groupchat protocols

The presence protocols

5.3 Adding groupchat support

5.3.1 Groupchat protocols

There are four critical things we need to be able to do in groupchat:

Join the group—Send an available<presence> to the groupchat group Jabber ID.Send messages to everyone in the group (broadcast messages)—Send a groupchat

Adding groupchat support 139

Send messages to one person in the group (private messages)—Send a groupchat

<message> to the person’s group Jabber ID using their group nickname. Sending private groupchat messages is often called "whispering".

Leave the group—Send an unavailable<presence> to the groupchat group

Jabber ID.

As you can see, two tasks use the presence update protocol, the other two the groupchat message protocol.

Groupchat groups are organized using a clever naming scheme. A normal user Jabber ID is of the form:

[user name]@[Jabber server]/[resource]

The groupchat protocol discussed here is groupchat 1.0 or more commonly referred to as basic groupchat. (The conference protocols are being developed as an advanced groupchat protocol that replaces basic groupchat. However, servers will probably support basic groupchat for a long time to come.)

A groupchat group Jabber ID takes the form: [group name]@[groupchat server]/[nickname]

To join a group, use the presence update protocol to send an available presence to the groupchat group, specifying your desired nickname. For example, figure 5.2 shows a sequence diagram of the group joining process. In order to sub- scribe to the “java-users” group on the groupchat server groups.shigeoka.com using the nickname “smirk,” I would send:

<presence to='[email protected]/smirk' from='[email protected]/work'/>

Figure 5.2 The user “iain” joins a group named “group” on the “host” Jabber server by sending a

<presence> packet with the desired group nickname of “smirk.” The server responds with a confirming

<presence> from the new group nickname and a welcome message. iain @host/work smirk <presence> to:group@host/smirk groupchat server hieu @host/work RunningMan <presence> from:group@host/smirk

<message> “smirk joined” from:group@host

140 CHAPTER 5

The presence protocols

The groupchat server associates my Jabber ID with the nickname “smirk” in its own internal group list. It then propagates the presence status of all group mem- bers to all group members. Since I’m now a group member the server will send me this update:

<presence to='[email protected]/work'

from='[email protected]/smirk'/>

The server will typically also send a welcome message to the group: <message to='[email protected]/work'

from='[email protected]' type='groupchat'>

<body>smirk has joined java-users</body> </message>

Notice that the sender is the groupchat server, not a groupchat participant. Now let’s say my friend Hieu also joins java-users (figure 5.3) by sending: <presence from='[email protected]/work'

to='[email protected]/RunningMan'/>

Figure 5.3 The user "hieu" joins the group named "group" with the nickname "RunningMan." The server must ensure that both "hieu" and "iain" are informed of the new list member "RunningMan." In addition, "hieu" is sent a <presence> packet from list member "smirk" so he is aware of the presence of all list members. Notice all <presence> and <message> packets are from groupchat addresses.

Hieu receives from the server:

<presence to='[email protected]/work' from='[email protected]/smirk'/> <presence to='[email protected]/work' from='[email protected]/RunningMan'/> <message to=’[email protected]/work' iain @host/work smirk <presence> to:group@host/RunningMan groupchat Server hieu @host/work RunningMan <presence> from:group@host/RunningMan

<message> “ RunningMan joined” from:group@host

<presence>

from:group@host/ RunningMan

<message> “ RunningMan joined” from:group@host

<presence> from:group@host/smirk

Adding groupchat support 141 from='[email protected]'

type='groupchat'>

<body>RunningMan has joined java-users</body> </message>

I also receive the presence update (my status doesn’t change so the server doesn’t have to update my presence):

<presence to='[email protected]/work'

from='[email protected]/RunningMan'/> <message to='[email protected]/work'

from='[email protected]' type='groupchat'>

<body>RunningMan has joined java-users</body> </message>

I don’t know RunningMan is Hieu, nor does Hieu know that I’m smirk. The groupchat server keeps track of the group member nicknames and hides our real identities. I can send messages directly to RunningMan without knowing Hieu’s real Jabber ID:

<message to='[email protected]/RunningMan' from='[email protected]/work'

type='groupchat'> <body>Hi RunningMan!</body> </message>

The message goes to the groupchat server (figure 5.4) The server looks up the “RunningMan” nickname to find Hieu’s Jabber ID and my Jabber ID to find the “smirk” nickname. The server rewrites the addresses in the message packet and sends it to Hieu. Hieu receives:

<message to='[email protected]/work'

from='[email protected]/smirk' type='groupchat'>

<body>Hi RunningMan!</body> </message>

Figure 5.4 User "iain" sends a private groupchat message to list member "RunningMan." Notice how the groupchat server adjusts the sender and recipient addresses of the message.

iain @host/work smirk groupchat Server hieu @host/work RunningMan <message> “Hi RunningMan!”

from: iain @host/work to: group@host/RunningMan

<message> “Hi RunningMan!” from: group@host/smirk to: hieu @host/work

142 CHAPTER 5

The presence protocols

Hieu can send a message to the entire group (figure 5.5) by addressing it to the group’s Jabber ID: <message to='[email protected]' from='[email protected]/work' type='groupchat'> <body>Anyone there?</body> </message>

Figure 5.5 User "hieu" sends a groupchat message to all group members. Once again, notice how the groupchat server adjusts sender and recipient addresses of the message.

The ser ver will for ward the message to ever yone in the group, rewriting the sender address to use Hieu’s group nickname. Hieu is part of the group so he will receive the response:

<message to='[email protected]/work'

from='[email protected]/RunningMan' type='groupchat'>

<body>Anyone there?</body> </message>

I’m also in the group so I get a copy: <message to='[email protected]/work'

from='[email protected]/RunningMan' type='groupchat'>

<body>Anyone there?</body> </message>

Hieu realizes this is a boring group with only one other person in it and decides to leave (figure 5.6). To leave a group, the user’s presence is changed to unavailable. Hieu can do this by sending the following packet:

iain @host/work smirk groupchat Server hieu @host/work RunningMan

<message> “Anyone there?” from: group@host/RunningMan

to: iain @host/work

<message> “Anyone there?” from: hieu @host/work to: group@host <message> “Anyone there?” from: group@host/RunningMan to: hieu@host/work

Adding groupchat support 143 <presence to='[email protected]/RunningMan'

from='[email protected]/work' type='unavailable'/>

Figure 5.6 User "hieu" leaves the group by sending an "unavailable" <presence> packet to the groupchat server. The server updates all list members with the change in presence, and sends a "goodbye" message to remaining list members.

Hieu receives a confirmation from the groupchat server: <presence to='[email protected]/work'

from='[email protected]/RunningMan' type='unavailable'/>

I receive the presence update packet and a message packet from the groupchat server: <presence to='[email protected]/work' from='[email protected]/RunningMan' type='unavailable'/> <message to='[email protected]/work' from='[email protected]' <body>RunningMan has left</body>

</message>

Knowing how Jabber message routing works, there are ways to implement a groupchat ser ver as a client chatbot application with minimal help from the server. The approach is very attractive for creating a Jabber server that runs on different machines, distributed across a network. However, to keep things simple and efficient,6 we’ll modify our ser ver to support groupchat. Embedding groupchat on the server is the most common approach for adding groupchat sup- port to a server. iain @host/work smirk groupchat Server hieu @host/work RunningMan <presence> “unavailable” from: group@host/RunningMan <presence> “unavailable” to: group@host/RunningMan <presence> “unavailable” from: group@host/ RunningMan <message> “ RunningMan left.”

144 CHAPTER 5

The presence protocols