Page updated Jan 16, 2024

Hub

Amplify Android v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Android to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

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.

1Amplify.Hub.subscribe(HubChannel.AUTH,
2 hubEvent -> {
3 if (hubEvent.getName().equals(InitializationStatus.SUCCEEDED.toString())) {
4 Log.i("AuthQuickstart", "Auth successfully initialized");
5 } else if (hubEvent.getName().equals(InitializationStatus.FAILED.toString())){
6 Log.i("AuthQuickstart", "Auth failed to succeed");
7 } else {
8 switch (AuthChannelEventName.valueOf(hubEvent.getName())) {
9 case SIGNED_IN:
10 Log.i("AuthQuickstart", "Auth just became signed in.");
11 break;
12 case SIGNED_OUT:
13 Log.i("AuthQuickstart", "Auth just became signed out.");
14 break;
15 case SESSION_EXPIRED:
16 Log.i("AuthQuickstart", "Auth session just expired.");
17 break;
18 case USER_DELETED:
19 Log.i("AuthQuickstart", "User has been deleted.");
20 break;
21 default:
22 Log.w("AuthQuickstart", "Unhandled Auth Event: " + AuthChannelEventName.valueOf(hubEvent.getName()));
23 break;
24 }
25 }
26 }
27);
1Amplify.Hub.subscribe(HubChannel.AUTH) { event ->
2 when (event.name) {
3 InitializationStatus.SUCCEEDED.toString() ->
4 Log.i("AuthQuickstart", "Auth successfully initialized")
5 InitializationStatus.FAILED.toString() ->
6 Log.i("AuthQuickstart", "Auth failed to succeed")
7 else -> when (AuthChannelEventName.valueOf(event.name)) {
8 AuthChannelEventName.SIGNED_IN ->
9 Log.i("AuthQuickstart", "Auth just became signed in")
10 AuthChannelEventName.SIGNED_OUT ->
11 Log.i("AuthQuickstart", "Auth just became signed out")
12 AuthChannelEventName.SESSION_EXPIRED ->
13 Log.i("AuthQuickstart", "Auth session just expired")
14 AuthChannelEventName.USER_DELETED ->
15 Log.i("AuthQuickstart", "User has been deleted")
16 else ->
17 Log.w("AuthQuickstart", "Unhandled Auth Event: ${event.name}")
18 }
19 }
20}
1Amplify.Hub.subscribe(HubChannel.AUTH).collect {
2 when (it.name) {
3 InitializationStatus.SUCCEEDED.toString() ->
4 Log.i("AuthQuickstart", "Auth successfully initialized")
5 InitializationStatus.FAILED.toString() ->
6 Log.i("AuthQuickstart", "Auth failed to succeed")
7 else -> when (AuthChannelEventName.valueOf(it.name)) {
8 AuthChannelEventName.SIGNED_IN ->
9 Log.i("AuthQuickstart", "Auth just became signed in.")
10 AuthChannelEventName.SIGNED_OUT ->
11 Log.i("AuthQuickstart", "Auth just became signed out.")
12 AuthChannelEventName.SESSION_EXPIRED ->
13 Log.i("AuthQuickstart", "Auth session just expired.")
14 AuthChannelEventName.USER_DELETED ->
15 Log.i("AuthQuickstart", "User has been deleted.")
16 }
17 }
18}
1RxAmplify.Hub.on(HubChannel.AUTH)
2 .map(HubEvent::getName)
3 .subscribe(name -> {
4 if (name.equals(InitializationStatus.SUCCEEDED.toString())) {
5 Log.i("AuthQuickstart", "Auth successfully initialized");
6 return;
7 } else if (name.equals(InitializationStatus.FAILED.toString())) {
8 Log.i("AuthQuickstart", "Auth failed to succeed");
9 return;
10 }
11 switch (AuthChannelEventName.valueOf(name)) {
12 case SIGNED_IN:
13 Log.i("AuthQuickstart", "Auth just became signed in.");
14 break;
15 case SIGNED_OUT:
16 Log.i("AuthQuickstart", "Auth just became signed out.");
17 break;
18 case SESSION_EXPIRED:
19 Log.i("AuthQuickstart", "Auth session just expired.");
20 break;
21 case USER_DELETED:
22 Log.i("AuthQuickstart", "User has been deleted.");
23 break;
24 default:
25 Log.w("AuthQuickstart", "Unhandled Auth Event: " + AuthChannelEventName.valueOf(name));
26 break;
27 }
28 });

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.

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