Home > Enterprise >  Can aws-amplify send events to react-native when the db changes?
Can aws-amplify send events to react-native when the db changes?

Time:06-23

Is it possible listening to db changes in Dynamo db from React Native?

The goal is to update automatically a value in React Native when something changes in the DB.

CodePudding user response:

Amplify supports it, but it is only possible if the origin of the change is done through the grapql api (from another user of your react native app), and not if the change in the db is is originated from somewere else (for example a lambda function). This is because in aws amplify it is aws AppSync that is pushing the change message.

If the origin of the change is from somewere else, you need to manually configure a lambda function and attach it as a trigger to dynamodb (this might be possible through the amplify cli, but I have never done it that way so I am not sure) and make the lambda function for example send a websocket/mqtt message to your app. But that is a lot of manually coding and configuring.

If the origin is from a user of your app, through the graphql api, amplify provides you with an out of the box subscription possibility. I use angular, and when use amplify I get a file called API.service.ts (not sure if it is the same for react native). In that file there would be an observable called for example OnCreateTodoListener.

You can use that that observable to subscribe to create events. Or similar for OnUpdateTodoListener etc.

The graphql query looks something like this (typescript version for angular):

OnUpdateTodoListener: Observable<
    SubscriptionResponse<OnUpdateTodoSubscription>
  > = API.graphql({
    query:
      `subscription OnUpdateTodo {
        onUpdateTodo {
          __typename
          id
        }
      }`
    }
  ) as Observable<SubscriptionResponse<OnUpdateTodoSubscription>>;

These listerers should already be availiable for you, unless you have manually configured your graphql schema model to not allow subscriptions.

Take a look at the docs for mor detailed information

  • Related