Friday 20 July 2012

XMPP CHAT ROOM MECHANISM # PART-2

In last we have seen how to get list of all available rooms in server. This post is about how to use those information to chat in a room.

Once user get list of all room. He needs to choose one room out of it to chat with. User will send presence to existing room. Here there is a little bit change in presence stanza. User needs to add nick name for the room. All other user will see him as his nick name rather than username. e.g.
<presence from='{user-jid}/{resource}' to='{room-id}@conference.domain/{nick-name}'></presence>
Server will send presence to all the members of the room. As if any user joins room later, he will also be sent the presence. This way server informs all users of chat room about joining and leaving event of any user.
<presence to='{user-jid}/{resource}' from='{room-id}@conference.domain/{nick-name}'>
<x xmlns='http://jabber.org/protocol/muc#user'>
<item role='participant' affiliation='none'></item>
</x>
</presence>
Once user gets successfull presence. I have mentioned "successfully", as there are some conditions when user can not enter in a room. In that case server will return a error presence stanza. You can get more details about such stanze from here. If some user have already sent some message the room. User will get all those message as following format whenever he will join a room.
<message to='{user-jid}/{resource}' from='{room-id}@conference.domain' type='groupchat'>
<subject>us.country</subject>
<delay stamp='2012-07-18T18:02:24.328Z' from='{room-id}@conference.domain' xmlns='urn:xmpp:delay'></delay>
<x stamp='20120718T18:02:24' from='{room-id}@conference.domain' xmlns='jabber:x:delay'></x>
</message>
Once user joins a room. He can start sending message to the room. Here user must provide type attribute for presence stanza having value as "groupchat". Describe as following xml.
<message type='groupchat' from = '{user-jid}/{resource}' to ='{room-id}@conference.domain'>
<body>dfdf</body>
</message>
Whatever message is being sent to chat room. Room will send that message to all current users of the room.
<message to='{user-jid}/{resource}' from='{room-id}@conference.domain/{nick-name}' type='groupchat'>
<body>dfdf</body>
</message>
Now when user gets tired of chatting with bunch of people. He would like to leave the room. He needs to send unavailable presence stanza to room's jid, including nickname also. Here nick name is important because a room identifies user based on their nick name. So when you join a room. It will lock that nick name for you. If you are leaving it, you need to make server to unlock the nick name.
<presence from='{user-jid}/{resource}' to='{room-id}@conference.domain/{nick-name}' type='unavailable'></presence>
Server will send user's presence details to all user who have joined the room. The user, who will join the room after that, will not get details about that user. Here if you notice. Server has added a role related information to stanza. Same type of information have been added when user sends presence stanza for joining. This role defines what can this user do with this room. I will post more details about that afterwards.
<presence to='{user-jid}/{resource}' type='unavailable' from='{room-id}@conference.domain/{nick-name}'>
<x xmlns='http://jabber.org/protocol/muc#user'>
<item role='none' affiliation='none'></item>
</x>
</presence>
Let me know if you have any queries regarding this post.

References : http://xmpp.org/extensions/xep-0045.html/

3 comments:

  1. hi thanks for nice article .can you write some more information about MUC. its difficult to read the xep-0045 document. write about role of admin,moderator,participants, thanks

    ReplyDelete
  2. In MUC i have doubts.
    1.How others will know if some one is removed or added
    2. How others will get the room members list

    ReplyDelete
  3. In MUC i have doubts.
    1.How others will know if some one is removed or added
    2. How others will get the room members list

    ReplyDelete