Page updated Jan 16, 2024

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.

Installation

import { Hub } from 'aws-amplify/utils';
1import { Hub } from 'aws-amplify/utils';

Working with the API

Channels

A channel is a logical group name that you use to organize messages and listen on. These are strings and completely up to you as the developer to define for dispatching or listening. However, while you can dispatch to any channel, Amplify protects certain channels and will flag a warning as sending unexpected payloads could have undesirable side effects (such as impacting authentication flows). The protected channels are currently:

  • core
  • auth
  • analytics
  • storage

Listening for messages

listen(channel: string, callback: HubCallback): StopListenerCallback is used to listen for messages that have been dispatched. You must provide either a named channel or a regular expression, along with a callback. In the case of a regular expression only dispatches which contain a message in their payload will be matched against your pattern. You can add multiple listeners to your application for different channels or patterns to listen for, or trap generic events and perform your own filtering.

import { Hub } from 'aws-amplify/utils'; class MyClass { constructor() { Hub.listen('auth', (data) => { const { payload } = data; this.onAuthEvent(payload); console.log( 'A new auth event has happened: ', data.payload.data.username + ' has ' + data.payload.event ); }); } onAuthEvent(payload) { // ... your implementation } }
1import { Hub } from 'aws-amplify/utils';
2
3class MyClass {
4 constructor() {
5 Hub.listen('auth', (data) => {
6 const { payload } = data;
7 this.onAuthEvent(payload);
8 console.log(
9 'A new auth event has happened: ',
10 data.payload.data.username + ' has ' + data.payload.event
11 );
12 });
13 }
14
15 onAuthEvent(payload) {
16 // ... your implementation
17 }
18}

Sending messages

Sending events to different channels is done with the dispatch function:

Hub.dispatch('DogsChannel', { event: 'buttonClick', data: { color: 'blue' }, message: '' }); setTimeout(() => { Hub.dispatch('CatsChannel', { event: 'drinkMilk', data: { breed: 'Persian', age: 5 }, message: `The cat ${cat.name} has finished her milk` }); }, 5000);
1Hub.dispatch('DogsChannel', {
2 event: 'buttonClick',
3 data: { color: 'blue' },
4 message: ''
5});
6
7setTimeout(() => {
8 Hub.dispatch('CatsChannel', {
9 event: 'drinkMilk',
10 data: {
11 breed: 'Persian',
12 age: 5
13 },
14 message: `The cat ${cat.name} has finished her milk`
15 });
16}, 5000);

Hub.dispatch(channel: string, payload: HubPayload) can be used to dispatch a HubPayload to a channel. The channel is a logical grouping for your organization while the HubPayload is a type defined as:

export type HubPayload = { event: string, data?: any, message?: string };
1export type HubPayload = {
2 event: string,
3 data?: any,
4 message?: string
5};

The event field is recommended to be a small string without spaces such as signIn or hang_up as it's useful for checking payload groupings. The data field is a freeform structure which many times is used for larger JSON objects or custom data types. Finally while message is optional.

Stop Listening

Hub provides a way to stop listening for messages with calling the result of the .listen function. This may be useful if you no longer need to receive messages in your application flow, as well as to avoid any memory leaks on low powered devices when you are sending large amounts of data through Hub on multiple channels.

To stop listening to a certain event, you need to wrap the listener function to a variable and call it once you no longer need it:

/* start listening for messages */ const hubListenerCancel = Hub.listen('auth', (data) => { console.log('Listening for all messages: ', data.payload.data); }); /* later */ hubListenerCancel(); // stop listening for messages
1/* start listening for messages */
2const hubListenerCancel = Hub.listen('auth', (data) => {
3 console.log('Listening for all messages: ', data.payload.data);
4});
5
6/* later */
7hubListenerCancel(); // stop listening for messages