Swarm Documentation API Reference

Swarm User Management

Swarm provides an easy way of managing users, friends, and communication.


Active User

Once a user has logged in (checkable with a call to Swarm.isLoggedIn()), the active user is available at Swarm.user. Swarm.user provides easy access methods for getting the user's friends list, pending friends, and sending new friend requests. Below is an example of how to get a user's friends list and check the online status of each friend (including which game or app the user is logged into). This could be used to facilitate matchmaking in a board game.

// Make sure someone is logged in...
if (Swarm.isLoggedIn()) {
   
    // Get the user's friend list.
    Swarm.user.getFriends(new GotFriendsCB() {
        public void gotFriends(List<SwarmUser> friends, List<SwarmUser> incomingRequests) {
            
            if (friends != null) {

                for (SwarmUser friend : friends) {

                    // See below for function
                    getFriendStatus(friend);
                }
            }
        }
    });
}

private void getFriendStatus(SwarmUser friend) {
    friend.getOnlineStatus(new GotUserStatusCB() {

        public void gotUserStatus(SwarmApplication app, boolean online) {
            // Handle user status...
        }
    });
}

Messaging

Messaging through Swarm is done on a global level (not app-specific), any messages that are sent are available to that user, regardless of what app they are in. Below is an example of how to send a message to a friend (note: users can only send messages to friends).

// We'll assume this is populated by the above examples.
SwarmUser friend = someFriend;

if (Swarm.isLoggedIn()) {
    Swarm.sendMessage(someFriend.userId, "Hello there friend!  What are you doing today?", null);
}

All of the user's messages are manage through their inbox, which is available with calls to SwarmMessageThread.getAllThreads(). A thread is a conversation between two users, and it gives easy access to the data necessary for showing and replying to the conversation.