Search code examples
iosswiftxmppxmppframework

Add multiple users in XMPP roster


How can I add multiple users in my roster? Right now I can add a single user at a time. But now I have to sync device contact with XMPP. Using backend API I can filter out the contacts which are registered with the application. Now adding them one by one to the roster is taking too much time.

So is there any quicker way to add multiple contacts to the roster?

I have already gone through many questions like this, but they are no help.

And is filtering device contact with backend API a good approach, or should I be doing something else?


Solution

  • XMPP is a complete XML based protocol, even through library doesn't have some methods, we can extend the library as per our need. So as you said that you want to add multiple rosters there are two approaches to achieve it: 1. Add some method into your XMPP client library or in your application and add multiple roster items as below:

    - (void)addUsers:(NSArray<XMPPJID *> *)jids withNickname:(NSArray<NSString *> *)optionalNames groups:(NSArray *)groups {
    
    if (jids == nil) return;
    XMPPJID *myJID = xmppStream.myJID;
    
    
    
    // Add the buddy to our roster
    //
    // <iq type="set">
    //   <query xmlns="jabber:iq:roster">
    //     <item jid="bareJID" name="optionalName">
    //      <group>family</group>
    //     </item>
    //   </query>
    // </iq>
    
    XMPPIQ *iq = [XMPPIQ iqWithType:@"set"];
    
    for (int i = 0; i < jids.count; i++) {
    
        XMPPJID *jid = jids[0];
        if ([myJID isEqualToJID:jid options:XMPPJIDCompareBare])
        {
            // You don't need to add yourself to the roster.
            // XMPP will automatically send you presence from all resources signed in under your username.
            //
            // E.g. If you sign in with [email protected]/home you'll automatically
            //    receive presence from [email protected]/work
    
            XMPPLogInfo(@"%@: %@ - Ignoring request to add myself to my own roster", [self class], THIS_METHOD);
            continue;
        }
        NSXMLElement *item = [NSXMLElement elementWithName:@"item"];
        [item addAttributeWithName:@"jid" stringValue:[jid bare]];
        NSString *optionalName = optionalNames[i];
        if(optionalName)
        {
            [item addAttributeWithName:@"name" stringValue:optionalName];
        }
    
        for (NSString *group in groups) {
            NSXMLElement *groupElement = [NSXMLElement elementWithName:@"group"];
            [groupElement setStringValue:group];
            [item addChild:groupElement];
        }
    
        NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:roster"];
        [query addChild:item];
        [iq addChild:query];
    }
    
    [xmppStream sendElement:iq];
    

    }

    1. Write some service api with some rabbitmq service at server side and it will insert multiple roster for a users and XMPP server will update you regarding roster update. I hope this answer will help you.