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
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.
public static String DidAuthenticate = "DidAuthenticate";
public static String UserOn = "UserOn";
public static String MessageReceived = "MessageReceived";
public static String MessageWillSend = "MessageWillSend";
public static String MessageSent = "MessageSent";
public static String IsNew_Boolean = "IsNew_Boolean";
public static String DidLogout = "DidLogout";
public static String WillLogout = "WillLogout";
public static String UserDidConnect = "UserDidConnect";
public static String UserWillDisconnect = "UserWillDisconnect";
public static String ContactWillBeAdded = "ContactWillBeAdded";
public static String ContactWasAdded = "ContactWasAdded";
public static String ContactWillBeDeleted = "ContactWillBeDeleted";
public static String ContactWasDeleted = "ContactWasDeleted";
Access data from the hook
Hooks can also provide data. You can access using the following:
public static String User = "User";
public static String Thread = "Thread";
public static String Message = "Message";
public static String Object = "Object";