Page updated Nov 14, 2023

Hub

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.name())) { Log.i("AuthQuickstart", "Auth successfully initialized"); } else if (hubEvent.getName().equals(InitializationStatus.FAILED.name())){ Log.i("AuthQuickstart", "Auth failed to succeed"); } else { String eventName = hubEvent.getName(); if (eventName.equals(SIGNED_IN.name())) { Log.i("AuthQuickstart", "Auth just became signed in."); } else if (eventName.equals(SIGNED_OUT.name())) { Log.i("AuthQuickstart", "Auth just became signed out."); } else if (eventName.equals(SESSION_EXPIRED.name())) { Log.i("AuthQuickstart", "Auth session just expired."); } else if (eventName.equals(USER_DELETED.name())) { Log.i("AuthQuickstart", "User has been deleted."); } else { Log.w("AuthQuickstart", "Unhandled Auth Event: " + eventName); } } } );
1Amplify.Hub.subscribe(HubChannel.AUTH,
2 hubEvent -> {
3 if (hubEvent.getName().equals(InitializationStatus.SUCCEEDED.name())) {
4 Log.i("AuthQuickstart", "Auth successfully initialized");
5 } else if (hubEvent.getName().equals(InitializationStatus.FAILED.name())){
6 Log.i("AuthQuickstart", "Auth failed to succeed");
7 } else {
8 String eventName = hubEvent.getName();
9 if (eventName.equals(SIGNED_IN.name())) {
10 Log.i("AuthQuickstart", "Auth just became signed in.");
11 }
12 else if (eventName.equals(SIGNED_OUT.name())) {
13 Log.i("AuthQuickstart", "Auth just became signed out.");
14 }
15 else if (eventName.equals(SESSION_EXPIRED.name())) {
16 Log.i("AuthQuickstart", "Auth session just expired.");
17 }
18 else if (eventName.equals(USER_DELETED.name())) {
19 Log.i("AuthQuickstart", "User has been deleted.");
20 }
21 else {
22 Log.w("AuthQuickstart", "Unhandled Auth Event: " + eventName);
23 }
24 }
25 }
26);

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);
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);