Class Roster
The roster lets you keep track of the availability ("presence") of other users. A roster also allows you to organize users into groups such as "Friends" and "Co-workers". Other IM systems refer to the roster as the buddy list, contact list, etc.
You can obtain a Roster instance for your connection via
getInstanceFor(XMPPConnection)
. A detailed description of the
protocol behind the Roster and Presence semantics can be found in
RFC 6120.
Roster Entries
Every user in a roster is represented by a RosterEntry, which consists of:- An XMPP address, aka. JID (e.g. jsmith@example.com).
- A name you've assigned to the user (e.g. "Joe").
- The list of groups in the roster that the entry belongs to. If the roster entry belongs to no groups, it's called an "unfiled entry".
Roster roster = Roster.getInstanceFor(connection);
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
System.out.println(entry);
}
Methods also exist to get individual entries, the list of unfiled entries, or
to get one or all roster groups.
Presence
Every entry in the roster has presence associated with it. The
getPresence(BareJid)
method will return a Presence object with the
user's presence or `null` if the user is not online or you are not subscribed
to the user's presence. _Note:_ Presence subscription is not tied to the
user being on the roster, and vice versa: You could be subscribed to a remote
users presence without the user in your roster, and a remote user can be in
your roster without any presence subscription relation.
A user either has a presence of online or offline. When a user is online, their presence may contain extended information such as what they are currently doing, whether they wish to be disturbed, etc. See the Presence class for further details.
Listening for Roster and Presence Changes
The typical use of the roster class is to display a tree view of groups and entries along with the current presence value of each entry. As an example, see the image showing a Roster in the Exodus XMPP client to the right.
The presence information will likely change often, and it's also possible for the roster entries to change or be deleted. To listen for changing roster and presence data, a RosterListener should be used. To be informed about all changes to the roster the RosterListener should be registered before logging into the XMPP server. The following code snippet registers a RosterListener with the Roster that prints any presence changes in the roster to standard out. A normal client would use similar code to update the roster UI with the changing information.
Roster roster = Roster.getInstanceFor(con);
roster.addRosterListener(new RosterListener() {
// Ignored events public void entriesAdded(Collection<String> addresses) {}
public void entriesDeleted(Collection<String> addresses) {
}
public void entriesUpdated(Collection<String> addresses) {
}
public void presenceChanged(Presence presence) {
System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
}
});
Note that in order to receive presence changed events you need to be
subscribed to the users presence. See the following section.
Adding Entries to the Roster
Rosters and presence use a permissions-based model where users must give permission before someone else can see their presence. This protects a user's privacy by making sure that only approved users are able to view their presence information. Therefore, when you add a new roster entry, you will not see the presence information until the other user accepts your request.
If another user requests a presence subscription, you must accept or reject that request. Smack handles presence subscription requests in one of three ways:
- Automatically accept all presence subscription requests
(
accept_all
) - Automatically reject all presence subscription requests
(
reject_all
) - Process presence subscription requests manually.
(
manual
)
The mode can be set using setSubscriptionMode(SubscriptionMode)
.
Simple clients normally use one of the automated subscription modes, while
full-featured clients should manually process subscription requests and let
the end-user accept or reject each request.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
An enumeration for the subscription mode options. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The initial maximum size of the map holding presence information of entities without a Roster entry. -
Method Summary
Modifier and TypeMethodDescriptionboolean
addPresenceEventListener
(PresenceEventListener presenceEventListener) Add aPresenceEventListener
.boolean
addRosterListener
(RosterListener rosterListener) Adds a listener to this roster.boolean
addRosterLoadedListener
(RosterLoadedListener rosterLoadedListener) Add a roster loaded listener.boolean
addSubscribeListener
(SubscribeListener subscribeListener) Add a subscribe listener, which is invoked on incoming subscription requests and ifRoster.SubscriptionMode
is set toRoster.SubscriptionMode.manual
.boolean
Returns true if the specified XMPP address is an entry in the roster.createGroup
(String name) Creates a new group.void
createItem
(BareJid jid, String name, String[] groups) Creates a new roster item.void
createItemAndRequestSubscription
(BareJid jid, String name, String[] groups) Creates a new roster entry and presence subscription.getAllPresences
(BareJid bareJid) Returns a List of Presence objects for all of a user's current presences if no presence information is available, such as when you are not subscribed to the user's presence updates.getAvailablePresences
(BareJid bareJid) Returns a List of all available Presence Objects for the given bare JID.static Roster.SubscriptionMode
Returns the default subscription processing mode to use when a new Roster is created.Returns a set of all entries in the roster, including entries that don't belong to any groups.void
getEntriesAndAddListener
(RosterListener rosterListener, RosterEntries rosterEntries) Add a roster listener and invoke the roster entries with all entries of the roster.Returns the roster entry associated with the given XMPP address ornull
if the user is not an entry in the roster.int
Returns a count of the entries in the roster.Returns the roster group with the specified name, ornull
if the group doesn't exist.int
Returns the number of the groups in the roster.Returns an unmodifiable collections of all the roster groups.static Roster
getInstanceFor
(XMPPConnection connection) Returns the roster for the user.getPresence
(BareJid jid) Returns the presence info for a particular user.getPresenceResource
(FullJid userWithResource) Returns the presence info for a particular user's resource, or unavailable presence if the user is offline or if no presence information is available, such as when you are not subscribed to the user's presence updates.getPresences
(BareJid jid) Returns a List of Presence objects for all of a user's current presences or an unavailable presence if the user is unavailable (offline) or if no presence information is available, such as when you are not subscribed to the user's presence updates.Returns the subscription processing mode, which dictates what action Smack will take when subscription requests from other users are made.Returns an unmodifiable set for the unfiled roster entries.int
Returns a count of the unfiled entries in the roster.boolean
iAmSubscribedTo
(Jid jid) Check if the XMPP entity this roster belongs to is subscribed to the presence of the given JID.boolean
isLoaded()
Check if the roster is loaded.boolean
Returns true if the roster will be loaded from the server when logging in.boolean
Check if the server supports roster versioning.boolean
Check if the given JID is subscribed to the user's presence.boolean
Check for subscription pre-approval support.void
preApprove
(BareJid user) Pre-approve user presence subscription.void
preApproveAndCreateEntry
(BareJid user, String name, String[] groups) Creates a new pre-approved roster entry and presence subscription.void
reload()
Reloads the entire roster from the server.void
Reload the roster and block until it is reloaded.void
removeEntry
(RosterEntry entry) Removes a roster entry from the roster.boolean
removePresenceEventListener
(PresenceEventListener presenceEventListener) boolean
removeRosterListener
(RosterListener rosterListener) Removes a listener from this roster.boolean
removeRosterLoadedListener
(RosterLoadedListener rosterLoadedListener) Remove a roster loaded listener.boolean
removeSubscribeListener
(SubscribeListener subscribeListener) Remove a subscribe listener.void
static void
setDefaultNonRosterPresenceMapMaxSize
(int maximumSize) Set the default maximum size of the non-Roster presence map.static void
setDefaultSubscriptionMode
(Roster.SubscriptionMode subscriptionMode) Sets the default subscription processing mode to use when a new Roster is created.void
setNonRosterPresenceMapMaxSize
(int maximumSize) Set the maximum size of the non-Roster presence map.void
setRosterLoadedAtLogin
(boolean rosterLoadedAtLogin) Sets if the roster will be loaded from the server when logging in.static void
setRosterLoadedAtLoginDefault
(boolean rosterLoadedAtLoginDefault) Sets if the roster will be loaded from the server when logging in for newly created instances ofRoster
.boolean
setRosterStore
(RosterStore rosterStore) Set the roster store, may cause a roster reload.void
setSubscriptionMode
(Roster.SubscriptionMode subscriptionMode) Sets the subscription processing mode, which dictates what action Smack will take when subscription requests from other users are made.Methods inherited from class org.jivesoftware.smack.Manager
connection, getAuthenticatedConnectionOrThrow, schedule, schedule, scheduleBlocking
-
Field Details
-
INITIAL_DEFAULT_NON_ROSTER_PRESENCE_MAP_SIZE
The initial maximum size of the map holding presence information of entities without a Roster entry. Currently 1024.- See Also:
-
-
Method Details
-
getInstanceFor
Returns the roster for the user.This method will never return
null
, instead if the user has not yet logged into the server all modifying methods of the returned roster object likecreateItemAndRequestSubscription(BareJid, String, String[])
,removeEntry(RosterEntry)
, etc. except adding or removingRosterListener
s will throw an IllegalStateException.- Parameters:
connection
- the connection the roster should be retrieved for.- Returns:
- the user's roster.
-
getDefaultSubscriptionMode
Returns the default subscription processing mode to use when a new Roster is created. The subscription processing mode dictates what action Smack will take when subscription requests from other users are made. The default subscription mode isRoster.SubscriptionMode.reject_all
.- Returns:
- the default subscription mode to use for new Rosters
-
setDefaultSubscriptionMode
Sets the default subscription processing mode to use when a new Roster is created. The subscription processing mode dictates what action Smack will take when subscription requests from other users are made. The default subscription mode isRoster.SubscriptionMode.reject_all
.- Parameters:
subscriptionMode
- the default subscription mode to use for new Rosters.
-
getSubscriptionMode
Returns the subscription processing mode, which dictates what action Smack will take when subscription requests from other users are made. The default subscription mode isRoster.SubscriptionMode.reject_all
.If using the manual mode, a PacketListener should be registered that listens for Presence packets that have a type of
Presence.Type.subscribe
.- Returns:
- the subscription mode.
-
setSubscriptionMode
Sets the subscription processing mode, which dictates what action Smack will take when subscription requests from other users are made. The default subscription mode isRoster.SubscriptionMode.reject_all
.If using the manual mode, a PacketListener should be registered that listens for Presence packets that have a type of
Presence.Type.subscribe
.- Parameters:
subscriptionMode
- the subscription mode.
-
reload
public void reload() throws SmackException.NotLoggedInException, SmackException.NotConnectedException, InterruptedExceptionReloads the entire roster from the server. This is an asynchronous operation, which means the method will return immediately, and the roster will be reloaded at a later point when the server responds to the reload request.- Throws:
SmackException.NotLoggedInException
- If not logged in.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
reloadAndWait
public void reloadAndWait() throws SmackException.NotLoggedInException, SmackException.NotConnectedException, InterruptedExceptionReload the roster and block until it is reloaded.- Throws:
SmackException.NotLoggedInException
- if the XMPP connection is not authenticated.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.- Since:
- 4.1
-
setRosterStore
Set the roster store, may cause a roster reload.- Parameters:
rosterStore
- TODO javadoc me please- Returns:
- true if the roster reload was initiated, false otherwise.
- Since:
- 4.1
-
isLoaded
Check if the roster is loaded.- Returns:
- true if the roster is loaded.
- Since:
- 4.1
-
addRosterListener
Adds a listener to this roster. The listener will be fired anytime one or more changes to the roster are pushed from the server.- Parameters:
rosterListener
- a roster listener.- Returns:
- true if the listener was not already added.
- See Also:
-
removeRosterListener
Removes a listener from this roster. The listener will be fired anytime one or more changes to the roster are pushed from the server.- Parameters:
rosterListener
- a roster listener.- Returns:
- true if the listener was active and got removed.
-
addRosterLoadedListener
Add a roster loaded listener. Roster loaded listeners are invoked once theRoster
was successfully loaded.- Parameters:
rosterLoadedListener
- the listener to add.- Returns:
- true if the listener was not already added.
- Since:
- 4.1
- See Also:
-
removeRosterLoadedListener
Remove a roster loaded listener.- Parameters:
rosterLoadedListener
- the listener to remove.- Returns:
- true if the listener was active and got removed.
- Since:
- 4.1
- See Also:
-
addPresenceEventListener
Add aPresenceEventListener
. Such a listener will be fired whenever certain presence events happen.Among those events are:
- 'available' presence received
- 'unavailable' presence received
- 'error' presence received
- 'subscribed' presence received
- 'unsubscribed' presence received
- Parameters:
presenceEventListener
- listener to add.- Returns:
- true if the listener was not already added.
-
removePresenceEventListener
-
createGroup
Creates a new group.Note: you must add at least one entry to the group for the group to be kept after a logout/login. This is due to the way that XMPP stores group information.
- Parameters:
name
- the name of the group.- Returns:
- a new group, or null if the group already exists
-
createItem
public void createItem(BareJid jid, String name, String[] groups) throws SmackException.NotLoggedInException, SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException Creates a new roster item. The server will asynchronously update the roster with the subscription status.There will be no presence subscription request. Consider using
createItemAndRequestSubscription(BareJid, String, String[])
if you also want to request a presence subscription from the contact.- Parameters:
jid
- the XMPP address of the contact (e.g. johndoe@jabber.org)name
- the nickname of the user.groups
- the list of group names the entry will belong to, ornull
if the roster entry won't belong to a group.- Throws:
SmackException.NoResponseException
- if there was no response from the server.XMPPException.XMPPErrorException
- if an XMPP exception occurs.SmackException.NotLoggedInException
- If not logged in.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.- Since:
- 4.4.0
-
createItemAndRequestSubscription
public void createItemAndRequestSubscription(BareJid jid, String name, String[] groups) throws SmackException.NotLoggedInException, SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException Creates a new roster entry and presence subscription. The server will asynchronously update the roster with the subscription status.- Parameters:
jid
- the XMPP address of the contact (e.g. johndoe@jabber.org)name
- the nickname of the user.groups
- the list of group names the entry will belong to, ornull
if the roster entry won't belong to a group.- Throws:
SmackException.NoResponseException
- if there was no response from the server.XMPPException.XMPPErrorException
- if an XMPP exception occurs.SmackException.NotLoggedInException
- If not logged in.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.- Since:
- 4.4.0
-
preApproveAndCreateEntry
public void preApproveAndCreateEntry(BareJid user, String name, String[] groups) throws SmackException.NotLoggedInException, SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.FeatureNotSupportedException Creates a new pre-approved roster entry and presence subscription. The server will asynchronously update the roster with the subscription status.- Parameters:
user
- the user. (e.g. johndoe@jabber.org)name
- the nickname of the user.groups
- the list of group names the entry will belong to, ornull
if the roster entry won't belong to a group.- Throws:
SmackException.NoResponseException
- if there was no response from the server.XMPPException.XMPPErrorException
- if an XMPP exception occurs.SmackException.NotLoggedInException
- if not logged in.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.SmackException.FeatureNotSupportedException
- if pre-approving is not supported.- Since:
- 4.2
-
preApprove
public void preApprove(BareJid user) throws SmackException.NotLoggedInException, SmackException.NotConnectedException, InterruptedException, SmackException.FeatureNotSupportedException Pre-approve user presence subscription.- Parameters:
user
- the user. (e.g. johndoe@jabber.org)- Throws:
SmackException.NotLoggedInException
- if not logged in.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.SmackException.FeatureNotSupportedException
- if pre-approving is not supported.- Since:
- 4.2
-
isSubscriptionPreApprovalSupported
Check for subscription pre-approval support.- Returns:
- true if subscription pre-approval is supported by the server.
- Throws:
SmackException.NotLoggedInException
- if not logged in.- Since:
- 4.2
-
sendSubscriptionRequest
-
addSubscribeListener
Add a subscribe listener, which is invoked on incoming subscription requests and ifRoster.SubscriptionMode
is set toRoster.SubscriptionMode.manual
. This also sets subscription mode toRoster.SubscriptionMode.manual
.- Parameters:
subscribeListener
- the subscribe listener to add.- Returns:
true
if the listener was not already added.- Since:
- 4.2
-
removeSubscribeListener
Remove a subscribe listener. Also restores the previous subscription mode state, if the last listener got removed.- Parameters:
subscribeListener
- TODO javadoc me please the subscribe listener to remove.- Returns:
true
if the listener registered and got removed.- Since:
- 4.2
-
removeEntry
public void removeEntry(RosterEntry entry) throws SmackException.NotLoggedInException, SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException Removes a roster entry from the roster. The roster entry will also be removed from the unfiled entries or from any roster group where it could belong and will no longer be part of the roster. Note that this is a synchronous call -- Smack must wait for the server to send an updated subscription status.- Parameters:
entry
- a roster entry.- Throws:
XMPPException.XMPPErrorException
- if an XMPP error occurs.SmackException.NotLoggedInException
- if not logged in.SmackException.NoResponseException
- SmackException if there was no response from the server.SmackException.NotConnectedException
- if the XMPP connection is not connected.InterruptedException
- if the calling thread was interrupted.
-
getEntryCount
Returns a count of the entries in the roster.- Returns:
- the number of entries in the roster.
-
getEntriesAndAddListener
Add a roster listener and invoke the roster entries with all entries of the roster.The method guarantees that the listener is only invoked after
RosterEntries.rosterEntries(Collection)
has been invoked, and that all roster events that happen whilerosterEntries(Collection)
is called are queued until the method returns.This guarantee makes this the ideal method to e.g. populate a UI element with the roster while installing a
RosterListener
to listen for subsequent roster events.- Parameters:
rosterListener
- the listener to installrosterEntries
- the roster entries callback interface- Since:
- 4.1
-
getEntries
Returns a set of all entries in the roster, including entries that don't belong to any groups.- Returns:
- all entries in the roster.
-
getUnfiledEntryCount
Returns a count of the unfiled entries in the roster. An unfiled entry is an entry that doesn't belong to any groups.- Returns:
- the number of unfiled entries in the roster.
-
getUnfiledEntries
Returns an unmodifiable set for the unfiled roster entries. An unfiled entry is an entry that doesn't belong to any groups.- Returns:
- the unfiled roster entries.
-
getEntry
Returns the roster entry associated with the given XMPP address ornull
if the user is not an entry in the roster.- Parameters:
jid
- the XMPP address of the user (e.g."jsmith@example.com"). The address could be in any valid format (e.g. "domain/resource", "user@domain" or "user@domain/resource").- Returns:
- the roster entry or
null
if it does not exist.
-
contains
Returns true if the specified XMPP address is an entry in the roster.- Parameters:
jid
- the XMPP address of the user (e.g."jsmith@example.com"). The address must be a bare JID e.g. "domain/resource" or "user@domain".- Returns:
- true if the XMPP address is an entry in the roster.
-
getGroup
Returns the roster group with the specified name, ornull
if the group doesn't exist.- Parameters:
name
- the name of the group.- Returns:
- the roster group with the specified name.
-
getGroupCount
Returns the number of the groups in the roster.- Returns:
- the number of groups in the roster.
-
getGroups
Returns an unmodifiable collections of all the roster groups.- Returns:
- an iterator for all roster groups.
-
getPresence
Returns the presence info for a particular user. If the user is offline, or if no presence data is available (such as when you are not subscribed to the user's presence updates), unavailable presence will be returned. If the user has several presences (one for each resource), then the presence with highest priority will be returned. If multiple presences have the same priority, the one with the "most available" presence mode will be returned. In order, that'sfree to chat
,available
,away
,extended away
, anddo not disturb
.Note that presence information is received asynchronously. So, just after logging in to the server, presence values for users in the roster may be unavailable even if they are actually online. In other words, the value returned by this method should only be treated as a snapshot in time, and may not accurately reflect other user's presence instant by instant. If you need to track presence over time, such as when showing a visual representation of the roster, consider using a
RosterListener
.- Parameters:
jid
- the XMPP address of the user (e.g."jsmith@example.com"). The address must be a bare JID e.g. "domain/resource" or "user@domain".- Returns:
- the user's current presence, or unavailable presence if the user is offline or if no presence information is available.
-
getPresenceResource
Returns the presence info for a particular user's resource, or unavailable presence if the user is offline or if no presence information is available, such as when you are not subscribed to the user's presence updates.- Parameters:
userWithResource
- a fully qualified XMPP ID including a resource (user@domain/resource).- Returns:
- the user's current presence, or unavailable presence if the user is offline or if no presence information is available.
-
getAllPresences
Returns a List of Presence objects for all of a user's current presences if no presence information is available, such as when you are not subscribed to the user's presence updates.- Parameters:
bareJid
- an XMPP ID, e.g. jdoe@example.com.- Returns:
- a List of Presence objects for all the user's current presences, or an unavailable presence if no presence information is available.
-
getAvailablePresences
Returns a List of all available Presence Objects for the given bare JID. If there are no available presences, then the empty list will be returned.- Parameters:
bareJid
- the bare JID from which the presences should be retrieved.- Returns:
- available presences for the bare JID.
-
getPresences
Returns a List of Presence objects for all of a user's current presences or an unavailable presence if the user is unavailable (offline) or if no presence information is available, such as when you are not subscribed to the user's presence updates.- Parameters:
jid
- an XMPP ID, e.g. jdoe@example.com.- Returns:
- a List of Presence objects for all the user's current presences, or an unavailable presence if the user is offline or if no presence information is available.
-
isSubscribedToMyPresence
Check if the given JID is subscribed to the user's presence.If the JID is subscribed to the user's presence then it is allowed to see the presence and will get notified about presence changes. Also returns true, if the JID is the service name of the XMPP connection (the "XMPP domain"), i.e. the XMPP service is treated like having an implicit subscription to the users presence.
Note that if the roster is not loaded, then this method will always return false.- Parameters:
jid
- TODO javadoc me please- Returns:
- true if the given JID is allowed to see the users presence.
- Since:
- 4.1
-
iAmSubscribedTo
Check if the XMPP entity this roster belongs to is subscribed to the presence of the given JID.- Parameters:
jid
- the jid to check.- Returns:
true
if we are subscribed to the presence of the given jid.- Since:
- 4.2
-
setRosterLoadedAtLoginDefault
Sets if the roster will be loaded from the server when logging in for newly created instances ofRoster
.- Parameters:
rosterLoadedAtLoginDefault
- if the roster will be loaded from the server when logging in.- Since:
- 4.1.7
- See Also:
-
setRosterLoadedAtLogin
Sets if the roster will be loaded from the server when logging in. This is the common behaviour for clients but sometimes clients may want to differ this or just never do it if not interested in rosters.- Parameters:
rosterLoadedAtLogin
- if the roster will be loaded from the server when logging in.
-
isRosterLoadedAtLogin
Returns true if the roster will be loaded from the server when logging in. This is the common behavior for clients but sometimes clients may want to differ this or just never do it if not interested in rosters.- Returns:
- true if the roster will be loaded from the server when logging in.
- See Also:
-
isRosterVersioningSupported
Check if the server supports roster versioning.- Returns:
- true if the server supports roster versioning, false otherwise.
-
setDefaultNonRosterPresenceMapMaxSize
Set the default maximum size of the non-Roster presence map.The roster will only store this many presence entries for entities non in the Roster. The default is 1024.
- Parameters:
maximumSize
- the maximum size- Since:
- 4.2
-
setNonRosterPresenceMapMaxSize
Set the maximum size of the non-Roster presence map.- Parameters:
maximumSize
- TODO javadoc me please- Since:
- 4.2
- See Also:
-