AWS AppSync: How to listen to change from DynamoDB (not by mutation)

Viewed 2209

I create AppSync API from Amplify Cli for my angular project.

The website can subscribe to OnChangeListner where DynamoDB is updated by mutation. It works.

async ngOnInit() {

   this.api.OnUpdateTableListener.subscribe({
      next: resp => {
        console.log("update !!!");
        console.log("update: " + JSON.stringify(resp));
        let update = resp.value.data.onUpdateTable;
        this.items = this.items.map(function(a) {
          return a.id === update.id ? update : a;
        });

      }
    });
}


async update(selectedItem) {
    const update = {
      id: selectedItem.id,
      status: !selectedItem.status
    };
    await this.api.UpdateTable(update);
  }

However, I noticed that if I updated DynamoDB manually, the subscription doesn't work at all.

Any suggestion on how to update DynamoDB (not by mutation) and AppSync subscriptions still work.

2 Answers

AppSync can only deliver messages via the subscription that are passed to it via mutation. One way to ensure that messages are always delivered via subscription if a change is made to a DynamoDB table is to add a Kinesis stream to the table that will emit events any time there has been a change made to the table. You can then have a Lambda funciton subscribe to this Kinesis stream and make the Mutation call to AppSync to have the modified data sent to connected clients via the Subscription. See diagram:

Diagram of Architecture

Related