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

Naveed Rafi picture Naveed Rafi · Jul 22, 2011 · Viewed 14k times · Source

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. :)

Answer

Keith OYS picture Keith OYS · Jun 12, 2014

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. :)