Amplify-js: DataStore observer does not trigger in other application instances logged in as the same user

Created on 5 May 2020  路  11Comments  路  Source: aws-amplify/amplify-js

Describe the bug
DataStore observer does not trigger in other application instances logged in as the same user.

If data is added to the first application instance, the observer fires. Further data is added to DynamoDB.

The data is only pulled into the second application instance on refresh. After the refresh, the new item is pulled into the local Indexed DB and the observer triggers.

To Reproduce
We see the problem in our application, but same behaviour following this simple grocery list example:
https://blog.logrocket.com/creating-offline-web-apps-with-aws-amplify-datastore/

Expected behavior
When data is added in one application, any other instance of the same application with the same logged in user receives the update via a subscribed observer.

Code Snippet
https://blog.logrocket.com/creating-offline-web-apps-with-aws-amplify-datastore/

What is Configured?
"@aws-amplify/auth": "^3.2.6",
"@aws-amplify/core": "^3.2.6",
"@aws-amplify/datastore": "^2.0.10",
"@aws-amplify/ui-react": "^0.2.5",

Thanks!

DataStore bug

Most helpful comment

Hi @manueliglesias - tried as you suggested and it works now :)

Thanks for the workaround!!
Steve

All 11 comments

Some further info. I get the same problem if I follow this example:
https://github.com/dabit3/amplify-datastore-example

adding in the subscriber to log to the console each time there's a new Post.

const subscription = DataStore.observe(Post).subscribe((msg) => { console.log(msg.model, msg.opType, msg.element) })

I see the log in the instance a new Post is created, but if I launch two instances of the application, the log doesn't get added in the second instance, when a new Post is added to the first instance.

UPDATE
With this project, adding Amplify.configure gets the subscriptions working:
Amplify.configure(aws_exports)

Wondering if the issue is for subscriptions with @auth rules on the model
@auth(rules: [{ allow: owner }])

Continuing with the example from https://github.com/dabit3/amplify-datastore-example

I added withAuthenticator HOC and retested, still working as expected across different instances.

I then modified the Post model and added @auth(rules: [{ allow: owner }]) (with a codegen/push) and it stopped working

type Post @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  title: String!
  rating: Int!
  status: PostStatus!
}

I tried taking out @auth(rules: [{ allow: owner }]) (with a codegen/push) and retrying and it still didn't work between instances

I added a new Model Post2 without any auth rules (with a codegen/push) and set the observer on Post2 , these worked between instances

I added a new Model Post3 with @auth(rules: [{ allow: owner }]) (with a codegen/push) and Post3 subscriptions, these don't work

In all cases I see the Keep Alive from appsync every 60 seconds

[DEBUG] 02:55.308 AWSAppSyncRealTimeProvider - subscription message from AWS AppSync RealTime: {"type":"ka"}
[DEBUG] 02:55.341 AWSAppSyncRealTimeProvider {id: "", observer: null, query: "", variables: {鈥}

It seems like my subscriptions don't work if the model has @auth(rules: [{ allow: owner }]).

Is there additional configuration needed or are subscriptions for data with an owner not yet supported?

Thanks.

Sounds like you've had a busy day troubleshooting this! So that I can reproduce this correctly, it sounds like things were working once you added Amplify.configure, but subscriptions don't work when there's an @auth rule?:

It seems like my subscriptions don't work if the model has @auth(rules: [{ allow: owner }]).

Hi Eric,

Thanks for looking into this.

That鈥檚 correct, when recreating with https://github.com/dabit3/amplify-datastore-example the subscriptions worked correctly after adding Amplify.configure, but without any @auth rule.

As soon as I added @auth to the model and updated it stopped working and continued to not work even after removing the rule.

Thanks again,
Steve

Hi @ericclemmons, any more info I can give to help recreate this?

Many thanks,
Steve

Hi @slewis77

Can you try your schema with @auth plus explicitly adding an owner field to your type?

type Post @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  title: String!
  rating: Int!
  status: PostStatus!
+  owner: String
}

You need to re-run modelgen and do a push first.

Hi @manueliglesias - tried as you suggested and it works now :)

Thanks for the workaround!!
Steve

Hi @slewis77

Can you try your schema with @auth plus explicitly adding an owner field to your type?

type Post @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  title: String!
  rating: Int!
  status: PostStatus!
+  owner: String
}

You need to re-run modelgen and do a push first.

I had the same problem, this solves it 馃憤

I searched for 2 days, thank you SO much!

this issue is still happening for me and adding the owner field didnt seem to fix it

Hi @slewis77

Can you try your schema with @auth plus explicitly adding an owner field to your type?

type Post @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  title: String!
  rating: Int!
  status: PostStatus!
+  owner: String
}

You need to re-run modelgen and do a push first.

Ran into the same problem. This fixed it.

This works for the same user but I've tried with a different user and it doesn't work is there any fix for different user?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

callmekatootie picture callmekatootie  路  3Comments

rayhaanq picture rayhaanq  路  3Comments

rygo6 picture rygo6  路  3Comments

epicfaace picture epicfaace  路  3Comments

karlmosenbacher picture karlmosenbacher  路  3Comments