XMPPFramework - How to create a MUC room and invite users?

14,457

Solution 1

After exploring various solutions, I've decided to compile and share my implementation here:

  1. Create an XMPP Room:

    XMPPRoomMemoryStorage *roomStorage = [[XMPPRoomMemoryStorage alloc] init];
    
    /** 
     * Remember to add 'conference' in your JID like this:
     * e.g. [email protected]
     */
    
    XMPPJID *roomJID = [XMPPJID jidWithString:@"[email protected]"];
    XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomStorage
                                                           jid:roomJID
                                                 dispatchQueue:dispatch_get_main_queue()];
    
    [xmppRoom activate:[self appDelegate].xmppStream];
    [xmppRoom addDelegate:self 
            delegateQueue:dispatch_get_main_queue()];
    
    [xmppRoom joinRoomUsingNickname:[self appDelegate].xmppStream.myJID.user 
                            history:nil 
                           password:nil];
    
  2. Check if room is successfully created in this delegate:

    - (void)xmppRoomDidCreate:(XMPPRoom *)sender
    
  3. Check if you've joined the room in this delegate:

    - (void)xmppRoomDidJoin:(XMPPRoom *)sender
    
  4. After room is created, fetch room configuration form:

    - (void)xmppRoomDidJoin:(XMPPRoom *)sender {
        [sender fetchConfigurationForm];
    }
    
  5. Configure your room

    /**
     * Necessary to prevent this message: 
     * "This room is locked from entry until configuration is confirmed."
     */
    
    - (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm 
    {
        NSXMLElement *newConfig = [configForm copy];
        NSArray *fields = [newConfig elementsForName:@"field"];
    
        for (NSXMLElement *field in fields) 
        {
            NSString *var = [field attributeStringValueForName:@"var"];
            // Make Room Persistent
            if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
                [field removeChildAtIndex:0];
                [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
            }
        }
    
        [sender configureRoomUsingOptions:newConfig];
    }
    

    References: XEP-0045: Multi-User Chat, Implement Group Chat

  6. Invite users

    - (void)xmppRoomDidJoin:(XMPPRoom *)sender 
    {
        /** 
         * You can read from an array containing participants in a for-loop 
         * and send multiple invites in the same way here
         */
    
        [sender inviteUser:[XMPPJID jidWithString:@"keithoys"] withMessage:@"Greetings!"];
    }
    

There, you've created a XMPP multi-user/group chat room, and invited a user. :)

Solution 2

Check the latest XMPPMUCLight & XMPPRoomLight its similar to Whatsapp and other today's trends social app rooms that don't get destroyed or members kicked when offline or no one in room.

Refer this documentation & mod from MongooseIM

Solution 3

I have the feeling that the first thing to do after alloc-init is to attach it to your xmppStream, so it can use xmppStream to send/receive messages.

More exactly:

XMPPRoom *room = [[XMPPRoom alloc] initWithRoomName:@"[email protected]/room" nickName:@"room"];
[room activate:[self xmppStream]];

//other things (create/config/...)
Share:
14,457
Naveed Rafi
Author by

Naveed Rafi

iPhone application developer.

Updated on June 10, 2022

Comments

  • Naveed Rafi
    Naveed Rafi about 2 years

    I am using Robbiehanson's iOS XMPPFramework. I am trying to create a MUC room and invite a user to the group chat room but it is not working.

    I am using the following code:

    XMPPRoom *room = [[XMPPRoom alloc] initWithRoomName:@"[email protected]/room" nickName:@"room"];
    [room createOrJoinRoom];
    [room sendInstantRoomConfig];
    [room setInvitedUser:@"[email protected]"];
    [room activate:[self xmppStream]];    
    [room inviteUser:jid1 withMessage:@"hello please join."];
    [room sendMessage:@"HELLO"];
    

    The user [email protected] should receive the invite message but nothing is happening.

    Any help will be appreciated. :)

  • Keith OYS
    Keith OYS about 10 years
    @NaveedRafi You're certainly most welcome. I hope this helps other XMPP users too. :-)
  • Rohit Mandiwal
    Rohit Mandiwal almost 10 years
    Thanks! is there a way to set password for room? I wanna to make private room.
  • Keith OYS
    Keith OYS almost 10 years
    @rohitmandiwal My pleasure! You can make a password-protected MUC room via this line as seen above - [xmppRoom joinRoomUsingNickname:[self appDelegate].xmppStream.myJID.user history:nil password:@"myPassword"];
  • Mangesh
    Mangesh over 9 years
    Hello All, Thanks you all and starckoverflow, I am able to create group and send Invitations to other with Both Storage (Core data & Memory Storage). Issue is when I create Second group it removes first group data from Core data storage and Also How can we auto join other user ?
  • Daljeet
    Daljeet over 9 years
    @KeithOYS - thanks so much for this code. I am unable to understand the step 3 where the user is joining the room. How do I get to know that the user has joined the room or not. Also if you could help us understand how do we receive and send messages once we have implemented this. Thanks a lot in advance for your help.
  • Kishore Suthar
    Kishore Suthar about 9 years
    I want to add other user to this group without sending invitation , what should i do for this
  • Charan Giri
    Charan Giri about 9 years
    I'm unable to create can you help me with this
  • Kaushik Movaliya
    Kaushik Movaliya over 8 years
    Hi, thanks for your answer, I am getting multiple messages when I join already created room using joinRoomUsingNickname method, can anyone help why it happens ?
  • Himanshu Mahajan
    Himanshu Mahajan over 8 years
    @Keith - I am unable to sent or received the message in MUC. I have created one room and user joins the room. I can see joined user on open fire server but I am not able to exchange the messages.
  • Himanshu Mahajan
    Himanshu Mahajan over 8 years
    @Keith - Can you please check my code and specify where I am wrong stackoverflow.com/questions/35156933/…
  • Bhavin Bhadani
    Bhavin Bhadani over 8 years
    @suthar did you solve this... how to add other user to this group without sending invitation ..
  • Usama Sadiq
    Usama Sadiq almost 8 years
    Any one please see this link what i am doing wrong here stackoverflow.com/questions/38895012/…
  • Usama Sadiq
    Usama Sadiq almost 8 years
    My didRecieveInvitation is not being called
  • May Phyu
    May Phyu about 7 years
    @KeithOYS, Can you help me this problem stackoverflow.com/questions/44172852/…
  • May Phyu
    May Phyu about 7 years
    can you help me this problem stackoverflow.com/questions/44172852/… ?