Skip to content

Latest commit

 

History

History
72 lines (57 loc) · 1.59 KB

hub_guide.md

File metadata and controls

72 lines (57 loc) · 1.59 KB

Hub

AWS Amplify has a lightweight Pub-Sub system called Hub. It is used to share events between modules and components.

Usage

Import

import { Hub } from 'aws-amplify';

Dispatch an event

Hub.dispatch('auth', { event: 'signIn', data: user }, 'Auth');

Listen to a channel

import { Hub, Logger } from 'aws-amplify';

const logger = new Logger('MyClass');

class MyClass {
    constructor() {
        Hub.listen('auth', this, 'MyListener');
    }

    onHubCapsule(capsule) {
        const { channel, payload, source } = capsule;
        logger.debug(channel, payload, source);
    }
}

Channel

AWS Amplify Auth publish in auth channel when 'signIn', 'signUp', and 'signOut' happens. You may create your listener to act upon event notifications.

import { Hub, Logger } from 'aws-amplify';

const logger = new Logger('MyClass');

class MyClass {
    constructor() {
        Hub.listen('auth', this, 'MyListener');
    }

    onHubCapsule(capsule) {
        const { channel, payload } = capsule;
        if (channel === 'auth') { onAuthEvent(payload); }
    }

    onAuthEvent(payload) {
        const { event, data } = payload;
        switch (event) {
            case 'signIn':
                logger.debug('user signed in');
                break;
            case 'signUp':
                logger.debug('user signed up');
                break;
            case 'signOut':
                logger.debug('user signed out');
                break;
            case 'signIn_failure':
                logger.debug('user sign in failed');
                break;
        }
    }
}