Contact List
Learn how to create, update, manage, and add users to your contact list.
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 our Key Concepts page to get an overall understanding of 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 our Setup page for more details.
- Create a user session to be able to use QuickBlox functionality. See our Authentication page to learn how to do it.
- Connect to the Chat server. See our Connection page to learn how to do it.
Access contact list
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
}
}
Add user to your contact list
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) {
};
Confirm the contact request
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) {
};
Reject the contact request
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) {
};
Remove user from the contact list
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
}
}
Contact list updates
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'.
};
Updated over 3 years ago