title: Create chatroom with XMPP framework and Ejabberd in iOS
body: <p>Basically I'm trying to create a chatroom with all registered users on my domain using a ejabberd server. So a user can see all other online registered users on that domain when he enters the chatroom.</p>
  
  <p>Until now I've only been able to make all the users 'friends / buddies' visible and deliver a notification when a friend goes online or offline with the help of the XMPP framework:</p>
  
  <pre><code>- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence {
  // a buddy went offline/online
  NSString *presenceType = [presence type]; // online/offline
  NSString *myUsername = [[sender myJID] user];
  NSString *presenceFromUser = [[presence from] user];
  if (![presenceFromUser isEqualToString:myUsername]) {
      if ([presenceType isEqualToString:@"available"]) {
          [_chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"chat.denederlandsewateren.nl"]];
      } else if ([presenceType isEqualToString:@"unavailable"]) {
          [_chatDelegate buddyWentOffline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"chat.denederlandsewateren.nl"]];
      }
  }
  }
  </code></pre>
  
  <p>I'm able to get a list with all online registered users but I don't know how to notify the user when somebody goes online or offline.</p>
  
  <pre><code>- (void)getAllRegisteredUsers {
  
  xmppRosterMemStorage = [[XMPPRosterMemoryStorage alloc] init];
  xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterMemStorage
                                           dispatchQueue:dispatch_get_main_queue()];
  [xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];
  xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = false;
  xmppRoster.autoFetchRoster = true;
  [xmppRoster activate:xmppStream];
  [xmppRoster fetchRoster];
  
  NSError *error = [[NSError alloc] init];
  NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"&lt;query xmlns='http://jabber.org/protocol/disco#items' node='all users'/&gt;"
                                                          error:&amp;error];
  XMPPIQ *iq = [XMPPIQ iqWithType:@"get"
                               to:[XMPPJID jidWithString:@"chat.denederlandsewateren.nl"]
                        elementID:[xmppStream generateUUID] child:query];
  [xmppStream sendElement:iq];
  </code></pre>
  
  <p>}</p>
  
  <pre><code>- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
  {
  NSXMLElement *queryElement = [iq elementForName: @"query" xmlns:     
  
  @"http://jabber.org/protocol/disco#items"];
  
  if (queryElement) {
      NSArray *itemElements = [queryElement elementsForName: @"item"];
      NSMutableArray *mArray = [[NSMutableArray alloc] init];
      for (int i=0; i&lt;[itemElements count]; i++) {
  
          NSString *jid=[[[itemElements objectAtIndex:i] attributeForName:@"jid"] stringValue];
          [mArray addObject:jid];
          [xmppRoster addUser:[XMPPJID jidWithString:jid] withNickname:[[jid componentsSeparatedByString:@"@"] objectAtIndex:0]];
  
      }
  
  
  
  }
  </code></pre>
  
  <p>How can I create a chatroom where the user can see all online registered users on one domain using a Ejabberd server and the XMPP framework?</p>