The Contact List API is rather straightforward. User A sends a request to become “friends” with user B. User B accepts the friend request. And now user A and B appear in each other’s roster.
Visit Key Concepts page to learn the most important QuickBlox concepts.
Before you begin
- Register a QuickBlox account. This is a matter of a few minutes and you will be able to use this account to build your apps.
- Configure QuickBlox SDK for your app. Check out Setup page for more details.
- Create a user session to be able to use QuickBlox functionality. See Authentication page to learn how to do it.
- Connect to the Chat server. See Connection page to learn how to do it.
You can access the contact list on your login to chat. The contact list object will be returned in callback. Also, the following function gives you an access to contact list.
try {
QB.chat.roster.get(function(contactlist) {
});
} catch (e) {
if (e.name === 'ChatNotConnectedError') {
// not connected to chat
}
}
To add a user to the contact list, use the add()
method.
var userId = 34;
try {
QB.chat.roster.add(userId, function() {
});
} catch (e) {
if (e.name === 'ChatNotConnectedError') {
// not connected to chat
}
}
Maximum number of contacts is 300.
Another user will receive a request to be added to the contact list. The onSubscribeListener
callback will be called.
QB.chat.onSubscribeListener = function(userId) {
};
To confirm the request, use the confirm()
method.
try {
QB.chat.roster.confirm(userId, function() {
});
} catch (e) {
if (e.name === 'ChatNotConnectedError') {
// not connected to chat
}
}
A user will be informed that you have accepted the contact request by the onConfirmSubscribeListener
callback.
QB.chat.onConfirmSubscribeListener = function(userId) {
};
To reject the request, use the reject()
method.
try {
QB.chat.roster.reject(userId, function() {
});
} catch (e) {
if (e.name === 'ChatNotConnectedError') {
// not connected to chat
}
}
A user will be informed that you have declined the contact request by onRejectSubscribeListener
callback.
QB.chat.onRejectSubscribeListener = function(userId) {
};
To remove a previously added user from the contact list, use the following method.
try {
QB.chat.roster.remove(userId, function() {
});
} catch (e) {
if (e.name === 'ChatNotConnectedError') {
// not connected to chat
}
}
You can also track contact list updates in real-time by using the code snippet below.
QB.chat.onContactListListener = function(userId, type) {
// type - if a user left the chat, type will be 'unavailable'.
// Otherwise - 'available'.
};