API Cheat Sheet
A quick reference guide to the Chat SDK API

Code Examples

The demo projects on Github contain some useful code examples.

Configuration

The Chat SDK can be configured using the configuration service.
Java
Swift
Objective C
1
Config config = ChatSDK.config();
2
​
3
// Example
4
config.setGoogleMaps("Google Maps API Key");
Copied!
1
let config: BConfiguration = BChatSDK.config()
2
​
3
// Example
4
config.googleMapsApiKey = "Google Maps API Key"
Copied!
1
BConfiguration * config = BChatSDK.config;
2
​
3
// Example
4
config.googleMapsApiKey = @"Google Maps API Key";
Copied!

Messaging Server API

The Chat SDK uses a series of services to make requests to the messaging server. These services are contained in a class called NetworkAdapter.
Java
Swift
Objective C
1
BaseNetworkAdapter a = ChatSDK.shared().a();
Copied!
1
let networkAdapter = BChatSDK.shared()?.networkAdapter
Copied!
1
id<PNetworkAdapter> networkAdapter = BChatSDK.shared.networkAdapter;
Copied!
Each of these services contains methods to make requests to the server. Here is a full list of the services available:
Java
Swift
Objective C
1
// Core Methods
2
CoreHandler core = networkAdapter.core;
3
AuthenticationHandler auth = networkAdapter.auth;
4
ThreadHandler thread = networkAdapter.thread;
5
ImageMessageHandler imageMessage = networkAdapter.imageMessage;
6
LocationMessageHandler locationMessage = networkAdapter.locationMessage;
7
ContactHandler contact = networkAdapter.contact;
8
SearchHandler search = networkAdapter.search;
9
PublicThreadHandler publicThread = networkAdapter.publicThread;
10
​
11
// Free modules
12
PushHandler push = networkAdapter.push;
13
UploadHandler upload = networkAdapter.upload;
14
​
15
// Paid modules
16
VideoMessageHandler videoMessage = networkAdapter.videoMessage;
17
AudioMessageHandler audioMessage = networkAdapter.audioMessage;
18
TypingIndicatorHandler typingIndicator = networkAdapter.typingIndicator;
19
LastOnlineHandler lastOnline = networkAdapter.lastOnline;
20
BlockingHandler blocking = networkAdapter.blocking;
21
NearbyUsersHandler nearbyUsers = networkAdapter.nearbyUsers;
22
ReadReceiptHandler readReceipts = networkAdapter.readReceipts;
23
StickerMessageHandler stickerMessage = networkAdapter.stickerMessage;
24
FileMessageHandler fileMessage = networkAdapter.fileMessage;
25
EncryptionHandler encryption = networkAdapter.encryption;
Copied!
1
// Core Methods
2
let core: PCoreHandler = networkAdapter.core()
3
let auth: PAuthenticationHandler = networkAdapter.auth()
4
let thread: PThreadHandler = networkAdapter.thread()
5
let imageMessage: PImageMessageHandler = networkAdapter.imageMessage()
6
let locationMessage: PLocationMessageHandler = networkAdapter.locationMessage()
7
let contact: PContactHandler = networkAdapter.contact()
8
let search: PSearchHandler = networkAdapter.search()
9
let publicThread: PPublicThreadHandler = networkAdapter.publicThread()
10
​
11
// Free modules
12
let push: PPushHandler = networkAdapter.push()
13
let upload: PUploadHandler = networkAdapter.upload()
14
​
15
// Paid modules
16
let videoMessage: PVideoMessageHandler = networkAdapter.videoMessage()
17
let audioMessage: PAudioMessageHandler = networkAdapter.audioMessage()
18
let typingIndicator: PTypingIndicatorHandler = networkAdapter.typingIndicator()
19
let lastOnline: PLastOnlineHandler = networkAdapter.lastOnline()
20
let blocking: PBlockingHandler = networkAdapter.blocking()
21
let nearbyUsers: PNearbyUsersHandler = networkAdapter.nearbyUsers()
22
let readReceipt: PReadReceiptHandler = networkAdapter.readReceipt()
23
let stickerMessage: PStickerMessageHandler = networkAdapter.stickerMessage()
24
let fileMessage: PFileMessageHandler = networkAdapter.fileMessage()
25
let encryption: PEncryptionHandler = networkAdapter.encryption()
Copied!
1
// Core Methods
2
id<PCoreHandler> core = networkAdapter.core;
3
id<PAuthenticationHandler> auth = networkAdapter.auth;
4
id<PThreadHandler> thread = networkAdapter.thread;
5
id<PImageMessageHandler> imageMessage = networkAdapter.imageMessage;
6
id<PLocationMessageHandler> locationMessage = networkAdapter.locationMessage;
7
id<PContactHandler> contact = networkAdapter.contact;
8
id<PSearchHandler> search = networkAdapter.search;
9
id<PPublicThreadHandler> publicThread = networkAdapter.publicThread;
10
​
11
// Free modules
12
id<PPushHandler> push = networkAdapter.push;
13
id<PUploadHandler> upload = networkAdapter.upload;
14
​
15
// Paid modules
16
id<PVideoMessageHandler> videoMessage = networkAdapter.videoMessage;
17
id<PAudioMessageHandler> audioMessage = networkAdapter.audioMessage;
18
id<PTypingIndicatorHandler> typingIndicator = networkAdapter.typingIndicator;
19
id<PLastOnlineHandler> lastOnline = networkAdapter.lastOnline;
20
id<PBlockingHandler> blocking = networkAdapter.blocking;
21
id<PNearbyUsersHandler> nearbyUsers = networkAdapter.nearbyUsers;
22
id<PReadReceiptHandler> readReceipt = networkAdapter.readReceipt;
23
id<PStickerMessageHandler> stickerMessage = networkAdapter.stickerMessage;
24
id<PFileMessageHandler> fileMessage = networkAdapter.fileMessage;
25
id<PEncryptionHandler> encryption = networkAdapter.encryption;
Copied!

Examples:

Core Service - Methods connected with the current user
Java
Swift
Objective C
1
// Push the user's profile data to the server
2
ChatSDK.core().pushUser().subscribe(() -> {
3
​
4
}, throwable -> {
5
​
6
});
7
​
8
// Get the current user
9
User user = ChatSDK.core().currentUser();
10
​
11
// Get a user and update their proifle from the server
12
ChatSDK.core().getUserForEntityID("User Entity ID").subscribe(otherUser -> {
13
​
14
}, throwable -> {
15
​
16
});
Copied!
1
// Push the user's profile data to the server
2
BChatSDK.core().pushUser()?.thenOnMain({ success in
3
4
return success
5
}, { error in
6
7
return error
8
})
9
​
10
// Get the current user
11
let user: PUser = BChatSDK.currentUser();
12
​
13
// Get a user and update their proifle from the server
14
let otherUser: PUser = BChatSDK.core().user(forEntityID: "User Entity ID")
Copied!
1
// Push the user's profile data to the server
2
[BChatSDK.core pushUser].thenOnMain(^id(id success) {
3
​
4
return nil;
5
}, ^id(NSError * error) {
6
7
return nil;
8
});
9
​
10
// Get the current user
11
id<PUser> user = BChatSDK.currentUser;
12
​
13
// Get a user and update their proifle from the server
14
id<PUser> otherUser = [BChatSDK.core userForEntityID:@"User Entity ID"];
Copied!
Auth Service - Methods for Authentication
Java
Swift
Objective C
1
// Login
2
ChatSDK.auth().authenticate(AccountDetails.username("Username", "Password")).subscribe(() -> {
3
​
4
}, throwable -> {
5
​
6
});
7
​
8
// Check if user is logged in
9
boolean isLoggedIn = ChatSDK.auth().isAuthenticated();
10
​
11
// Log out
12
ChatSDK.auth().logout().subscribe();
Copied!
1
// Login
2
BChatSDK.auth().authenticate(BAccountDetails.username("username", password: "password"))?.thenOnMain({ success in
3
4
return success
5
}, { error in
6
7
return error
8
})
9
​
10
// Check if user is logged in
11
let isLoggedIn = BChatSDK.auth().isAuthenticated()
12
​
13
// Log out
14
BChatSDK.auth().logout()
Copied!
1
// Login
2
[BChatSDK.auth authenticate:[BAccountDetails username:@"username" password:@"password"]].thenOnMain(^id(id success) {
3
4
return nil;
5
}, ^id(NSError * error) {
6
7
return nil;
8
});
9
​
10
// Check if user is logged in
11
bool isLoggedIn = BChatSDK.auth.isAuthenticated;
12
​
13
// Log out
14
[BChatSDK.auth logout];
Copied!
Thread Service - Methods related to Conversation Threads
Java
Swift
Objective C
1
// Create thread
2
User otherUser = ChatSDK.core().getUserNowForEntityID("EntityID");
3
ChatSDK.thread().createThread("Name", Collections.singletonList(otherUser)).subscribe(thread1 -> {
4
​
5
// Send a message
6
ChatSDK.thread().sendMessageWithText("Hi", thread1).subscribe();
7
​
8
});
9
​
10
// Get a list of public threads
11
List<Thread> threads = ChatSDK.thread().getThreads(ThreadType.Private1to1);
Copied!
1
// Create thread
2
BChatSDK.thread().createThread(withUsers: [otherUser], name: "Name", threadCreated: { error, thread in
3
4
// Send a message
5
BChatSDK.thread().sendMessage(withText: "Hi", withThreadEntityID: thread?.entityID())
6
​
7
})
8
9
// Get a list of public threads
10
let threads = BChatSDK.thread().threads(with: bThreadType1to1)
Copied!
1
// Create thread
2
[BChatSDK.thread createThreadWithUsers:@[otherUser] name:@"Name" threadCreated:^(NSError * error, id<PThread> thread) {
3
4
// Send a message
5
[BChatSDK.thread sendMessageWithText:@"Hi" withThreadEntityID:thread.entityID];
6
​
7
}];
8
​
9
// Get a list of public threads
10
NSArray * threads = [BChatSDK.thread threadsWithType:bThreadType1to1];
Copied!

Messaging Server Notifications

Since messaging happens in realtime, we also need to get notifications from the server. This happens through several mechanisms. For more details see:
Some available for Android and others for iOS:
1. Event Bus (Android)
Java
EventType.java
1
Predicate<NetworkEvent> filter = NetworkEvent.filterType(EventType.MessageAdded, EventType.MessageRemoved);
2
​
3
Disposable d = ChatSDK.events()
4
.source()
5
.filter(filter)
6
.subscribe(networkEvent -> {
7
​
8
// Handle Event Here
9
if (networkEvent.getMessage() != null) {
10
Logger.debug(networkEvent.getMessage().getText());
11
}
12
13
});
14
​
15
// Stop listening
16
d.dispose();
Copied!
1
public enum EventType {
2
ThreadAdded,
3
ThreadRemoved,
4
ThreadDetailsUpdated,
5
ThreadMetaUpdated,
6
MessageAdded,
7
MessageUpdated,
8
MessageRemoved,
9
MessageSendStatusUpdated,
10
ThreadUsersUpdated,
11
ThreadUserRoleUpdated,
12
UserMetaUpdated,
13
UserPresenceUpdated,
14
ContactAdded,
15
ContactDeleted,
16
ContactsUpdated,
17
TypingStateUpdated,
18
Logout,
19
ThreadRead,
20
MessageReadReceiptUpdated,
21
NearbyUserAdded,
22
NearbyUserMoved,
23
NearbyUserRemoved,
24
NearbyUsersUpdated,
25
Error
26
}
Copied!
2. Hooks
Java
Swift
Objective C
1
// Hooks
2
ChatSDK.hook().addHook(Hook.sync(data -> {
3
Message message = (Message) data.get(HookEvent.Message);
4
}), HookEvent.MessageReceived);
5
​
6
// Asynchronous code
7
ChatSDK.hook().addHook(Hook.async(data -> Completable.create(emitter -> {
8
// ... Async code here
9
emitter.onComplete();
10
})), HookEvent.MessageReceived);
Copied!
1
BChatSDK.hook().add(BHook.init(onMain: { dict in
2
if let message = dict?[bHook_PMessage] as? PMessage {
3
print(message.text())
4
}
5
}, weight: 10), withNames: [bHookMessageRecieved, bHookMessageWillSend])
Copied!
1
[BChatSDK.hook addHook:[BHook hookOnMain:^(NSDictionary * dict) {
2
id<PMessage> message = dict[bHook_PMessage];
3
if (message) {
4
NSLog(@"%@", message.text);
5
}
6
} weight:10] withNames:@[bHookMessageRecieved, bHookMessageWillSend]];
7
​
Copied!
Available Hooks
Android
iOS
1
public static String DidAuthenticate = "DidAuthenticate";
2
​
3
public static String UserOn = "UserOn";
4
​
5
public static String MessageReceived = "MessageReceived";
6
public static String MessageWillSend = "MessageWillSend";
7
public static String MessageSent = "MessageSent";
8
public static String IsNew_Boolean = "IsNew_Boolean";
9
​
10
public static String DidLogout = "DidLogout";
11
public static String WillLogout = "WillLogout";
12
​
13
public static String UserDidConnect = "UserDidConnect";
14
public static String UserWillDisconnect = "UserWillDisconnect";
15
​
16
public static String ContactWillBeAdded = "ContactWillBeAdded";
17
public static String ContactWasAdded = "ContactWasAdded";
18
public static String ContactWillBeDeleted = "ContactWillBeDeleted";
19
public static String ContactWasDeleted = "ContactWasDeleted";
Copied!
1
#define bHookDidAuthenticate @"bHookDidAuthenticate"
2
​
3
#define bHook_AuthenticationType @"bHook_AuthenticationType"
4
#define bHook_AuthenticationTypeLogin @"login"
5
#define bHook_AuthenticationTypeSignUp @"signup"
6
#define bHook_AuthenticationTypeCached @"cached"
7
​
8
#define bHookWillLogout @"bHookWillLogout"
9
#define bHookDidLogout @"bHookDidLogout"
10
​
11
#define bHookUserOn @"bHookUserOn"
12
​
13
#define bHookContactWillBeAdded @"bHookContactWillBeAdded"
14
#define bHookContactWasAdded @"bHookContactWasAdded"
15
#define bHookContactWillBeDeleted @"bHookContactWillBeDeleted"
16
#define bHookContactWasDeleted @"bHookContactWasDeleted"
17
​
18
#define bHookMessageRecieved @"bHookMessageRecieved"
19
​
20
#define bHookMessageWillSend @"bHookMessageWillSend"
21
#define bHookMessageSending @"bHookMessageSending"
22
#define bHookMessageDidSend @"bHookMessageDidSend"
23
#define bHookMessageWillUpload @"bHookMessageWillUpload"
24
#define bHookMessageDidUpload @"bHookMessageDidUpload"
25
​
26
#define bHookMessageWillBeDeleted @"bHookMessageWillBeDeleted"
27
#define bHookMessageWasDeleted @"bHookMessageWasDeleted"
28
#define bHookAllMessagesDeleted @"bHookAllMessagesDeleted"
29
​
30
#define bHookThreadAdded @"bHookThreadAdded"
31
#define bHookThreadRemoved @"bHookThreadRemoved"
32
#define bHookThreadUpdated @"bHookThreadUpdated"
33
#define bHookThreadUsersUpdated @"bHookThreadUsersUpdated"
34
#define bHookThreadUserRoleUpdated @"bHookThreadUserRoleUpdated"
35
​
36
#define bHookWillPushUser @"bHookWillPushUser"
37
#define bHookUserUpdated @"bHookUserUpdated"
38
​
39
#define bHookServerPingFailed @"bHookServerPingFailed"
40
​
41
#define bHookServerConnectionStatusUpdated @"bHookServerConnectionStatusUpdated"
42
​
43
#define bHookGlobalAlertMessage @"bHookGlobalAlertMessage"
44
#define bHookSettingsUpdated @"bHookSettingsUpdated"
45
​
46
#define bHookInternetConnectivityDidChange @"bHookInternetConnectivityDidChange"
47
#define bHookUserWillDisconnect @"bHookUserWillDisconnect"
Copied!
3. Notification Center (iOS Deprecated)
Swift
Objective C
Available Events
1
NotificationCenter.default.addObserver(forName: bNotificationLogout, object: nil, queue: nil, using: { notification in
2
3
})
Copied!
1
[[NSNotificationCenter defaultCenter] addObserverForName:bNotificationLogout object:Nil queue:Nil usingBlock:^(NSNotification * notification) {
2
​
3
}];
Copied!
1
#define bNotificationMessageUpdated @"bNMessageUpdated"
2
#define bNotificationMessageUpdatedKeyMessage @"bNMessageUpdatedKeyMessage"
3
​
4
#define bNotificationFlaggedMessageAdded @"bNFlaggedMessageAdded"
5
#define bNotificationFlaggedMessageAdded_PMessage @"bNFlaggedMessageAdded_PMessage"
6
​
7
#define bNotificationFlaggedMessageRemoved @"bNFlaggedMessageRemoved"
8
#define bNotificationFlaggedMessageRemoved_PMessage @"bNFlaggedMessageRemoved_PMessage"
9
​
10
#define bNotificationThreadRead @"bNThreadRead"
11
#define bNotificationBadgeUpdated @"bNBadgeUpdated"
12
​
13
#define bNotificationPresentChatView @"bNPresentChatView"
14
#define bNotificationPresentChatView_PThread @"bNPresentChatView_PThread"
15
​
16
#define bNotificationThreadLastMessageUpdated @"bNThreadLastMessageUpdated"
17
#define bNotificationThreadLastMessageUpdated_Text @"bNThreadLastMessageUpdated_Text"
18
​
19
#define bNotificationReadReceiptUpdated @"bNReadReceiptUpdated"
20
#define bNotificationReadReceiptUpdatedKeyMessage @"bNReadReceiptUpdatedKeyMessage"
21
​
22
#define bNotificationTypingStateChanged @"bNTypingStateChanged"
23
#define bNotificationTypingStateChangedKeyThread @"bNTypingStateChangedKeyThread"
24
#define bNotificationTypingStateChangedKeyMessage @"bNTypingStateChangedKeyMessage"
25
​
26
#define bNotificationAuthenticationComplete @"bNAuthenticationComplete"
Copied!

UI Modification Service

Chat SDK provides a way to modify the user interface without forking the library. This can be achieved using the UI server.
Java
Swift
Objective C
1
InterfaceAdapter interfaceAdapter = ChatSDK.ui();
Copied!
1
let interfaceAdapter: PInterfaceAdapter = BChatSDK.ui()
Copied!
1
id<PInterfaceAdapter> interfaceAdapter = BChatSDK.ui;
Copied!
The UI service is used to inject views into the app. It also defines which tabs are displayed and much more. For example, you can override Activites, Fragments, and ViewControllers by subclassing them and injecting them using the UI service.
Java
Swift
Objective C
1
// Override an activity
2
ChatSDK.ui().setChatActivity(CustomChatActivity.class);
3
​
4
// Override a fragment
5
ChatSDK.ui().setProfileFragmentProvider(user -> {
6
ProfileFragment fragment = new AProfileFragment();
7
fragment.setUser(user);
8
return fragment;
9
});
Copied!
1
// Override the chat view controller
2
BChatSDK.ui().setChatViewController({ thread -> UIViewController in
3
return AChatViewController.init(thread: thread)
4
})
5
​
6
// Override the private threads tab
7
BChatSDK.ui().setPrivateThreadsViewController(APrivateThreadsViewController.init(nibName: nil, bundle: nil))
8
​
Copied!
1
// Override the chat view controller
2
[BChatSDK.ui setChatViewController: ^UIViewController *(id<PThread> thread) {
3
return [[AChatViewController alloc] initWithThread:thread];
4
}];
5
​
6
// Override the private threads tab
7
[BChatSDK.ui setPrivateThreadsViewController:[[APrivateThreadsViewController alloc] initWithNibName:nil bundle:nil]];
8
​
Copied!
For more details see:

Local Database

The Chat SDK has a powerful local database that is automatically synchronized with the messaging server. It can be accessed using the database service.
Java
Swift
Objective C
1
StorageManager storageManager = ChatSDK.db();
Copied!
1
let storageAdapter: PStorageAdapter = BChatSDK.db()
Copied!
1
id<PStorageAdapter> storageAdapter = BChatSDK.db;
Copied!
The database service can be used to fetch, create, and delete entities. For example:
Java
Swift
Objective C
1
// Create entity
2
User user = ChatSDK.db().createEntity(User.class);
3
​
4
// Fetch an entity with a given ID
5
Thread thread = ChatSDK.db().fetchEntityWithEntityID("threadEntityID", Thread.class);
6
​
7
// Fetch or create an entity with a given ID
8
User otherUser = ChatSDK.db().fetchOrCreateEntityWithEntityID(User.class, "userEntityID'");
Copied!
1
// Create entity
2
if let user: PUser = BChatSDK.db().createEntity(bUserEntity) as? PUser {
3
4
}
5
​
6
// Fetch an entity with a given ID
7
if let thread: PThread? = BChatSDK.db().fetchEntity(withID: "threadEntityID", withType: bThreadEntity) as? PThread {
8
9
}
10
​
11
// Fetch or create an entity with a given ID
12
if let otherUser: PUser = BChatSDK.db().fetchOrCreateEntity(withID: "userEntityID", withType: bUserEntity) as? PUser {
13
14
}
Copied!
1
// Create entity
2
id<PUser> user = [BChatSDK.db createEntity:bUserEntity];
3
​
4
// Fetch an entity with a given ID
5
id<PThread> thread = [BChatSDK.db fetchEntityWithID:@"threadEntityID" withType:bThreadEntity];
6
​
7
// Fetch or create an entity with a given ID
8
id<PUser> otherUser = [BChatSDK.db fetchOrCreateEntityWithID:@"userEntityID" withType:bUserEntity];
9
​
Copied!
Each entity then has many methods that can be used. For example:
Java
Swift
Objective C
1
user.setName("Test");
2
user.setAvatarURL("http://something.png");
3
​
4
List<User> users = thread.getUsers();
5
List<Message> messages = thread.getMessages();
6
​
7
// etc...
Copied!
1
user.setName("test");
2
user.setImageURL("http://something.png")
3
​
4
let users = thread?.users()
5
let messages = thread?.messagesOrderedByDateNewestFirst()
6
​
7
// etc...
Copied!
1
[user setName:@"Test"];
2
[user setImageURL:@"http://something.png"];
3
​
4
NSArray * users = thread.users;
5
NSArray * messages = thread.messagesOrderedByDateNewestFirst;
6
​
7
// etc...
8
​
Copied!
Note: Creating an entity locally doesn't automatically add it to the server or populate the data from the server. To do that you have to use the network adapter service.
Last modified 10mo ago