Broadcast message to a roster group

Hi all,

Can anyone show me how to broacast a message to all memeber in a roster group.

many thanks,

Trung

This is one way to do it…

// assuming you have a valid connection
          Roster roster = conn.getRoster();
          String groupName = "Friends";
          RosterGroup group = roster.getGroup(groupName);
          Iterator iter = group.getEntries();
          while (iter.hasNext()) {
               RosterEntry entry = (RosterEntry) iter.next();
               try {
                    conn.createChat(entry.getUser()).sendMessage("enter message here"); //alternatively this could be a message object
               } catch (XMPPException e) {
                    // handle exception
               }
          }

Hope this helps,

Angelo

Thanks Angelo,

I have thought of this approach but do you know of another way that a client only has to send one message to the server indicating that “this message is for the whole roster group” and the server will broadcast it to the whole roster group.

Regards,

trung.

I don’‘t think you can do that (at least I don’‘t know how). Nothing in the Smack API that I’'ve seen does that.

-Angelo

Thanks Angelo,

With the help of broadcast plugin, I have managed to broadcast message to a/or all roster group(s).

XMPPConnection con = new XMPPConnection(“localhost”, 5222);

String host = con.getServiceName();

Roster roster = con.getRoster();

Iterator rit = roster.getGroups();

while (rit.hasNext()) {

RosterGroup rg = rit.next();

String bcAdd = rg.getName()+"@broadcast." + host;

Chat newChat = con.createChat(bcAdd);

newChat.sendMessage("Any broadcast message to group: " + rg.getName());

}

Regards,

Trung.

Answered