Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 29, 2024

Hub

Amplify Android v1 is deprecated as of June 1st, 2024. No new features or bug fixes will be added. Dependencies may become outdated and potentially introduce compatibility issues.

Please use the latest version (v2) of Amplify Library for Android to get started. Refer to the upgrade guide for instructions on upgrading your application to the latest version.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for Android, you can access the documentation here.

Amplify has a local eventing system called Hub. It is a lightweight implementation of Publisher-Subscriber pattern, and is used to share data between modules and components in your app. Amplify uses Hub for different categories to communicate with one another when specific events occur, such as authentication events like a user sign-in or notification of a file download.

Working with the API

Listening for messages

Hub.subscribe(HubChannel hubChannel, HubSubscriber hubSubscriber) is used to listen for messages that have been dispatched. Additionally, you can pass a HubEventFilter in the overloaded subscribe method to further filter Hub events. You can add multiple listeners to your application for different combinations of channels/filters to listen for.

Amplify.Hub.subscribe(HubChannel.AUTH,
hubEvent -> {
if (hubEvent.getName().equals(InitializationStatus.SUCCEEDED.toString())) {
Log.i("AuthQuickstart", "Auth successfully initialized");
} else if (hubEvent.getName().equals(InitializationStatus.FAILED.toString())){
Log.i("AuthQuickstart", "Auth failed to succeed");
} else {
switch (AuthChannelEventName.valueOf(hubEvent.getName())) {
case SIGNED_IN:
Log.i("AuthQuickstart", "Auth just became signed in.");
break;
case SIGNED_OUT:
Log.i("AuthQuickstart", "Auth just became signed out.");
break;
case SESSION_EXPIRED:
Log.i("AuthQuickstart", "Auth session just expired.");
break;
case USER_DELETED:
Log.i("AuthQuickstart", "User has been deleted.");
break;
default:
Log.w("AuthQuickstart", "Unhandled Auth Event: " + AuthChannelEventName.valueOf(hubEvent.getName()));
break;
}
}
}
);
Amplify.Hub.subscribe(HubChannel.AUTH) { event ->
when (event.name) {
InitializationStatus.SUCCEEDED.toString() ->
Log.i("AuthQuickstart", "Auth successfully initialized")
InitializationStatus.FAILED.toString() ->
Log.i("AuthQuickstart", "Auth failed to succeed")
else -> when (AuthChannelEventName.valueOf(event.name)) {
AuthChannelEventName.SIGNED_IN ->
Log.i("AuthQuickstart", "Auth just became signed in")
AuthChannelEventName.SIGNED_OUT ->
Log.i("AuthQuickstart", "Auth just became signed out")
AuthChannelEventName.SESSION_EXPIRED ->
Log.i("AuthQuickstart", "Auth session just expired")
AuthChannelEventName.USER_DELETED ->
Log.i("AuthQuickstart", "User has been deleted")
else ->
Log.w("AuthQuickstart", "Unhandled Auth Event: ${event.name}")
}
}
}
Amplify.Hub.subscribe(HubChannel.AUTH).collect {
when (it.name) {
InitializationStatus.SUCCEEDED.toString() ->
Log.i("AuthQuickstart", "Auth successfully initialized")
InitializationStatus.FAILED.toString() ->
Log.i("AuthQuickstart", "Auth failed to succeed")
else -> when (AuthChannelEventName.valueOf(it.name)) {
AuthChannelEventName.SIGNED_IN ->
Log.i("AuthQuickstart", "Auth just became signed in.")
AuthChannelEventName.SIGNED_OUT ->
Log.i("AuthQuickstart", "Auth just became signed out.")
AuthChannelEventName.SESSION_EXPIRED ->
Log.i("AuthQuickstart", "Auth session just expired.")
AuthChannelEventName.USER_DELETED ->
Log.i("AuthQuickstart", "User has been deleted.")
}
}
}
RxAmplify.Hub.on(HubChannel.AUTH)
.map(HubEvent::getName)
.subscribe(name -> {
if (name.equals(InitializationStatus.SUCCEEDED.toString())) {
Log.i("AuthQuickstart", "Auth successfully initialized");
return;
} else if (name.equals(InitializationStatus.FAILED.toString())) {
Log.i("AuthQuickstart", "Auth failed to succeed");
return;
}
switch (AuthChannelEventName.valueOf(name)) {
case SIGNED_IN:
Log.i("AuthQuickstart", "Auth just became signed in.");
break;
case SIGNED_OUT:
Log.i("AuthQuickstart", "Auth just became signed out.");
break;
case SESSION_EXPIRED:
Log.i("AuthQuickstart", "Auth session just expired.");
break;
case USER_DELETED:
Log.i("AuthQuickstart", "User has been deleted.");
break;
default:
Log.w("AuthQuickstart", "Unhandled Auth Event: " + AuthChannelEventName.valueOf(name));
break;
}
});

Stop Listening

Hub provides a way to stop listening for messages. This is useful if you no longer need to receive messages in your application flow (ex: changing Activities or Fragments), as well as to avoid any memory leaks.

// Subscribe to Hub Auth events and capture the returned SubscriptionToken
SubscriptionToken subscriptionToken = Amplify.Hub.subscribe(HubChannel.AUTH, event -> {
handleAuthEvent(event);
});
// Unsubscribe by passing the captured SubscriptionToken
Amplify.Hub.unsubscribe(subscriptionToken);
// Subscribe to Hub Auth events and capture the returned SubscriptionToken
val subscriptionToken = Amplify.Hub.subscribe(HubChannel.AUTH) { event ->
handleAuthEvent(event)
}
// Unsubscribe by passing the captured SubscriptionToken
Amplify.Hub.unsubscribe(subscriptionToken)