19

I am working on the iOS chat client. Can anyone please help me with the Multi-User Chat?

I have implemented Robbiehanson's XMPPFramework.

Can anyone please let me know how to get list of group and create a group in server with this framework?

Thanks in advance.

Keith OYS
  • 2,285
  • 5
  • 32
  • 38
Satish
  • 1,012
  • 2
  • 15
  • 32
  • 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

3 Answers3

38

to get a list of rooms:

NSString* server = @"chat.shakespeare.lit"; //or whatever the server address for muc is
XMPPJID *servrJID = [XMPPJID jidWithString:server];
XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:servJID];
[iq addAttributeWithName:@"from" stringValue:[xmppStream myJID].full];
NSXMLElement *query = [NSXMLElement elementWithName:@"query"];
[query addAttributeWithName:@"xmlns" stringValue:@"http://jabber.org/protocol/disco#items"];
[iq addChild:query];
[xmppStream sendElement:iq];

check for response in delegate method:

- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq{
    DDLogVerbose(@"%@", [iq description]);
    return NO;
}

to join or create room

XMPPRoomMemoryStorage * _roomMemory = [[XMPPRoomMemoryStorage alloc]init];
NSString* roomID = @"roomExample@chat.shakespeare.lit";
XMPPJID * roomJID = [XMPPJID jidWithString:roomID];
XMPPRoom* xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:_roomMemory
                                             jid:roomJID
                                   dispatchQueue:dispatch_get_main_queue()];
[xmppRoom activate:self.xmppStream];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom joinRoomUsingNickname:@"myNickname"
                        history:nil
                       password:nil];

check for response in XMPPRoom delegate methods:

- (void)xmppRoomDidCreate:(XMPPRoom *)sender{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

- (void)xmppRoomDidJoin:(XMPPRoom *)sender{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

update

to configure a room:

after:

[xmppRoom joinRoomUsingNickname:self.xmppStream.myJID.user
                        history:history
                       password:nil];

add:

[xmppRoom fetchConfigurationForm];

and check response in:

- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm{
    DDLogVerbose(@"%@: %@ -> %@", THIS_FILE, THIS_METHOD, sender.roomJID.user);
}

Review the configForm object, and change as needed, then send it with [sender configureRoomUsingOptions:newConfig];

example: to change the configuration to make the room persistent you can add something like:

NSXMLElement *newConfig = [configForm copy];
NSArray* fields = [newConfig elementsForName:@"field"];
for (NSXMLElement *field in fields) {
    NSString *var = [field attributeStringValueForName:@"var"];
    if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
        [field removeChildAtIndex:0];
        [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
    }
}
[sender configureRoomUsingOptions:newConfig];

(i'm not familiar with NSXMLElement, so maybe there is a better way to change the value)

Flores Robles
  • 656
  • 7
  • 14
  • 1
    Thanks for your answer it did worked for me. Can you please let me know how to setup different type of room, I want to create persistent room which is like group in whatsapp, one can invite people and chat. Please let me know if you have any idea about it. – Satish Nov 09 '13 at 04:59
  • 1
    Look at `[xmppRoom configureRoomUsingOptions:(NSXMLElement *)roomConfigForm]`. To create the `NSXMLElement`with the room configuration you desire refer to: [link](http://xmpp.org/extensions/xep-0045.html#roomconfig) also look into example 159 – Flores Robles Nov 09 '13 at 17:30
  • Thanks for comment Flores, I did looked in the server document and read the things in it, but couldn't get the idea about how to configure it. I have tried to pass the form for configuration but it had no effect. If you have code or any other document would be great help... – Satish Nov 15 '13 at 05:28
  • Thanks for your comment Flores, It really helped me a lot. I have with you code it does gives me the list of all chat room but is there any way to get only room which user has created or he is member of it. – Satish Dec 03 '13 at 13:11
  • 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:30
  • @FloresRobles, where to write the code to get a list of rooms ? – Sushil Sharma Apr 20 '15 at 09:53
  • @FloresRobles Delegate methods are not calling – Charan Giri May 19 '15 at 03:17
  • @FloresRobles, Could 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:55
  • @FloresRobles Delegates methods are not calling.. is there is any methods are there to check whethere delegates methods called or not. not able to join please help – Mad Burea Aug 24 '17 at 06:43
3

Here is a Swift version:

func joinRoom(with jidString: String, delegate: XMPPRoomDelegate) -> XMPPRoom {

    let roomJID = XMPPJID(string: jidString)
    let roomStorage = XMPPRoomCoreDataStorage.sharedInstance()

    let room = XMPPRoom(roomStorage: roomStorage, jid: roomJID, dispatchQueue: DispatchQueue.main)!

        room.activate(xmppStream)

        room.addDelegate(delegate, delegateQueue: DispatchQueue.main)

        // If the room is not existing, server will create one.
        room.join(usingNickname: xmppStream.myJID.user, history: nil)

        return room
    }

    // MUCRoomDelegate
    public func xmppRoomDidCreate(_ sender: XMPPRoom!) {
        print("xmppRoomDidCreate")

        // I prefer configure right after created
        sender.fetchConfigurationForm()
    }

    public func xmppRoomDidJoin(_ sender: XMPPRoom!) {
        print("xmppRoomDidJoin")
    }

    public func xmppRoom(_ sender: XMPPRoom!, didFetchConfigurationForm configForm: DDXMLElement!) {
        print("didFetchConfigurationForm")

        let newForm = configForm.copy() as! DDXMLElement

        for field in newForm.elements(forName: "field") {

            if let _var = field.attributeStringValue(forName: "var") {

                switch _var {
                case "muc#roomconfig_persistentroom":
                    field.remove(forName: "value")
                    field.addChild(DDXMLElement(name: "value", numberValue: 1))

                case "muc#roomconfig_membersonly":
                    field.remove(forName: "value")
                    field.addChild(DDXMLElement(name: "value", numberValue: 1))

                // other configures
                default:
                    break
                }

            }

        }

        sender.configureRoom(usingOptions: newForm)
    }

    public func xmppRoom(_ sender: XMPPRoom!, didConfigure iqResult: XMPPIQ!) {
        print("didConfigure")
    }
dichen
  • 1,643
  • 14
  • 19
  • Can you help me this https://stackoverflow.com/questions/44172852/how-to-parse-xmppmessage-element-attribute-and-node-in-xmppframework-with-swif @dichen? – May Phyu May 25 '17 at 08:55
0
+(void)getGroupRooms{
    NSError *error = nil;
    NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"<query xmlns='http://jabber.org/protocol/disco#items'/>" error:&error];
    XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:[XMPPJID jidWithString:Jabber_groupChat_Domain_server] elementID:[[[PXMPPManager sharedInstance] xmppStream] generateUUID] child:query];
    [iq addAttributeWithName:@"from" stringValue:[[[PXMPPManager sharedInstance] xmppStream] myJID].full];
    [[[PXMPPManager sharedInstance] xmppStream] sendElement:iq];

//<iq type="get" 
//to="conference.cnr-uat.panamaxil.com" 
//id="DF27F28E-488D-4DAB-AA03-399A4CDE91B3" 
//from="919414184320@cnr-uat.panamaxil.com/iphone">
//<query xmlns="http://jabber.org/protocol/disco#items"/>
//</iq>
}

#pragma - mark XMPPStreamDelegate Methods

- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq{
//    NSLog(@"Did receive IQ");

    if([iq isResultIQ])
    {
        if([iq elementForName:@"query" xmlns:@"http://jabber.org/protocol/disco#items"])
        {
            NSLog(@"Jabber Server's Capabilities: %@", [iq XMLString]);

            NSXMLElement *queryElement = [iq elementForName:@"query" xmlns:@"http://jabber.org/protocol/disco#items"];
            NSArray *items = [queryElement elementsForName:@"item"];
            NSMutableArray *arrGroupName = [[NSMutableArray alloc] init];
            for (NSXMLElement *i in items) {
                NSString *roomName = [i attributeStringValueForName:@"name"];
                NSString *jidString = [i attributeStringValueForName:@"jid"];
                //XMPPJID *jid = [XMPPJID jidWithString:jidString];

                NSDictionary *dict = @{
                                       @"groupName" : roomName,
                                       @"groupJID" : jidString,
                                       };
                [arrGroupName addObject:dict];
            }

            [ConversationsModel saveGroupName:arrGroupName];
        }
    }

    return false;
}
Ravi Kumar
  • 1,356
  • 14
  • 22