Amplify-js: Typescript errors when using PubSub on ionic

Created on 3 May 2018  路  7Comments  路  Source: aws-amplify/amplify-js

Im trying to connect to the IOT device shadows via PubSub,

i got this type errors

Amplify.addPluggable(new AWSIoTProvider({
      aws_pubsub_region: 'eu-xxx-x',
      aws_pubsub_endpoint: 'wss://xxxxxxxx.iot.xx-xx-x.amazonaws.com/mqtt'
    }));
Typescript Error
Argument of type '{ aws_pubsub_region: string; aws_pubsub_endpoint: string; }' is not assignable to parameter of type 'MqttProvidertOptions'. Object literal may only specify known properties, and 'aws_pubsub_region' does not exist in type 'MqttProvidertOptions'.
PubSub bug pending-close-response-required tracked

Most helpful comment

Found a way to manually configure the AWSIoTProvider in Angular without errors.

Doing it the way specified in the documentation for PubSub will result in errors. However, if you configure PubSub in the same way the other services are configured, then it's all fine and the missing properties from the interface aren't a problem.

So for instance, in your AppComponent:

Amplify.configure({
    Auth: {
        // Auth config ...
    },
    PubSub: {
        aws_pubsub_region: 'YOUR REGION HERE',
        aws_pubsub_endpoint: 'YOUR ENDPOINT HERE'
    }
});

// Note no options are passed in to the provider constructor -- no compiler error
Amplify.addPluggable(new AWSIoTProvider());

after that you can subscribe to a topic and publish messages through the injected amplifyService without any issues.

import { AmplifyService } from 'aws-amplify-angular';

export class MyClass {
    constructor(private amplifyService: AmplifyService) {}

    subscribe() {
        this.amplifyService.pubsub().subscribe('myopic').subscribe(
            next: data => console.log('pubsub next(): ', data),
            error: error => console.log('pubsub error(): ', error),
            close: () => console.log('pub sub close()') 
        );
    }

    publish(message: string) {
        this.amplifyService.pubsub().publish('mytopic', { msg: message });
    }
}

All 7 comments

Adding those types to

export interface MqttProvidertOptions {
    clientId?: string;
    url?: string;
    aws_pubsub_region: string;
    aws_pubsub_endpoint: string;
}

Will result to

api-service.ts:77 Error: Missing connections info
    at AWSAppSyncProvider.subscribe (AWSAppSyncProvider.js:75)
    at PubSub.js:114
    at Array.map (<anonymous>)
    at PubSub.js:112
    at new Subscription (Observable.js:179)
    at Observable.subscribe (Observable.js:258)
    at ApiServiceProvider.webpackJsonp.91.ApiServiceProvider.getMqttSensorData (api-service.ts:75)
    at HubsPage.webpackJsonp.203.HubsPage.getMqttSensorData (hubs.ts:51)
    at Object.eval [as handleEvent] (HubsPage.html:23)
    at handleEvent (core.js:13589)

The issue was no connections set for the AppSync, but still the typescript error persists

Can I vote that this stays open until the interface definition is fixed as per your observation in the original post.

Found a way to manually configure the AWSIoTProvider in Angular without errors.

Doing it the way specified in the documentation for PubSub will result in errors. However, if you configure PubSub in the same way the other services are configured, then it's all fine and the missing properties from the interface aren't a problem.

So for instance, in your AppComponent:

Amplify.configure({
    Auth: {
        // Auth config ...
    },
    PubSub: {
        aws_pubsub_region: 'YOUR REGION HERE',
        aws_pubsub_endpoint: 'YOUR ENDPOINT HERE'
    }
});

// Note no options are passed in to the provider constructor -- no compiler error
Amplify.addPluggable(new AWSIoTProvider());

after that you can subscribe to a topic and publish messages through the injected amplifyService without any issues.

import { AmplifyService } from 'aws-amplify-angular';

export class MyClass {
    constructor(private amplifyService: AmplifyService) {}

    subscribe() {
        this.amplifyService.pubsub().subscribe('myopic').subscribe(
            next: data => console.log('pubsub next(): ', data),
            error: error => console.log('pubsub error(): ', error),
            close: () => console.log('pub sub close()') 
        );
    }

    publish(message: string) {
        this.amplifyService.pubsub().publish('mytopic', { msg: message });
    }
}

@Jun711 the PR needs a change that @manueliglesias will be updating soon.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TheRealRed7 picture TheRealRed7  路  3Comments

cosmosof picture cosmosof  路  3Comments

rygo6 picture rygo6  路  3Comments

guanzo picture guanzo  路  3Comments

cgarvis picture cgarvis  路  3Comments