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

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. 🙂

Leave a Comment