19

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:@"user101@conference.jabber.org/room" nickName:@"room"];
[room createOrJoinRoom];
[room sendInstantRoomConfig];
[room setInvitedUser:@"ABC@jabber.org"];
[room activate:[self xmppStream]];    
[room inviteUser:jid1 withMessage:@"hello please join."];
[room sendMessage:@"HELLO"];

The user ABC@jabber.org should receive the invite message but nothing is happening.

Any help will be appreciated. :)

Keith OYS
  • 2,285
  • 5
  • 32
  • 38
Naveed Rafi
  • 2,503
  • 5
  • 32
  • 40

3 Answers3

35

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. uniqueRoomJID@conference.yourserverdomain
     */
    
    XMPPJID *roomJID = [XMPPJID jidWithString:@"chat@conference.shakespeare"];
    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. :)

Community
  • 1
  • 1
Keith OYS
  • 2,285
  • 5
  • 32
  • 38
  • @NaveedRafi You're certainly most welcome. I hope this helps other XMPP users too. :-) – Keith OYS Jun 17 '14 at 02:24
  • Thanks! is there a way to set password for room? I wanna to make private room. – Rohit Mandiwal Jul 24 '14 at 01:58
  • 2
    @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"];` – Keith OYS Jul 27 '14 at 16:21
  • 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 ? – Mangesh Oct 03 '14 at 14:32
  • @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. – Daljeet Feb 24 '15 at 18:37
  • I want to add other user to this group without sending invitation , what should i do for this – Kishore Suthar Apr 15 '15 at 09:43
  • I'm unable to create can you help me with this – Charan Giri May 23 '15 at 04:02
  • 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 ? – Kaushik Movaliya Dec 17 '15 at 10:00
  • @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 Feb 02 '16 at 14:38
  • @Keith - Can you please check my code and specify where I am wrong http://stackoverflow.com/questions/35156933/unable-to-send-or-receive-messages-in-multi-user-chat-xamppframework – Himanshu Mahajan Feb 02 '16 at 15:04
  • @suthar did you solve this... how to add other user to this group without sending invitation .. – Bhavin Bhadani Mar 09 '16 at 07:42
  • Any one please see this link what i am doing wrong here http://stackoverflow.com/questions/38895012/didreceiveinvitation-is-not-being-called-in-xmppframework-and-swift-2 – Usama Sadiq Aug 11 '16 at 11:50
  • My didRecieveInvitation is not being called – Usama Sadiq Aug 11 '16 at 11:51
  • @KeithOYS, Can you help me this problem https://stackoverflow.com/questions/44172852/how-to-parse-xmppmessage-element-attribute-and-node-in-xmppframework-with-swif – May Phyu May 25 '17 at 08:56
1

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

codelover
  • 1,113
  • 10
  • 28
  • can you help me this problem https://stackoverflow.com/questions/44172852/how-to-parse-xmppmessage-element-attribute-and-node-in-xmppframework-with-swif ? – May Phyu May 25 '17 at 08:56
1

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:@"user101@conference.jabber.org/room" nickName:@"room"];
[room activate:[self xmppStream]];

//other things (create/config/...)
Codrut
  • 11
  • 1