Events
How to handle notifications from the messaging server
Events
Chat SDK has two main event systems.
EventHandler
You can access the event handler using:
ChatSDK.events()There are then three main methods:
ChatSDK.events().sourceOnMain().subscribe(event -> {
});Listen to events on the main thread.
ChatSDK.events().source().subscribe(event -> {
});Listen to events on a background thread.
ChatSDK.events().errorSourceOnMain().subscribe(throwable -> {
});Listen for global errors.
List of available events
You can look at the EventType enum. Here is a summary of the events that exist at the present time:
Filtering
If you want to listen for a particular event type you can use the following:
Accessing Event Object
Different event types have different associated objects. For example, a message added event will also provide the message:
Removing the listener
If you want to remove an event listener you can use:
Hooks
The second way to access events is using the hook system. The difference is that events are read-only whereas with hooks, you can change the functionality of the core library. For example, using a hook, you could intercept the message sent event, make an API call to remove offensive language, modify the message object and then resume message sending.
Sync vs Async
Before demonstrating the API, there is an important thing to understand. Hooks come in two forms, synchronous and asynchronous.
Synchronous: This means that processing will be resumed as soon as your block of code finishes. This should be used if you are not calling an external API
Asynchronous: This means that processing will be suspended until your completable completes. This is good if you need to call an external server API
Sync
Add a synchronous hook:
Async
This time we are using a completable. That means that execution in the main app will be paused until you call emitter.onComplete(). So in this case, the app will pause until YourLongRunningAPI.call() has finished.
Be aware, if you don't call
onComplete()the app will never continue execution.
Available Hooks
You can see a list of the available hooks in the HookEvent class.
Access data from the hook
Hooks can also provide data. You can access using the following:
So:
Remove a hook
You can also remove a hook:
Last updated
Was this helpful?