Moralis live queries not working

I am trying to use live queries to monitor my DB for change in particular row and perform some action accordingly but I couldn’t use that feature. Because of that I need to reload my webapp to see the changes. So help me plzzz.

It works if you reload the app?

What doesn’t work? Any error? What is the code that you use for live queries?

I have referred live queries in moralis docs before proceeding with my custom code but I couldn’t get the results as shown on the documentation.

Live queries

can you share more of the code?

you will have to use on update instead of on create with a nitro server for some tables.

To be more specific… in the docs - create subscription, event handling codes
. What is a nitro server ?? Please show me a sample code

any new created server is a nitro server now

Am trying to create something like facebook friend request model. One user sends request and at the other side it gets notified for the approval. So is it possible to use Nitro queries to achieve it. Without live queries I have to reload the page to see the updates.

Yes If you create a subscription with on update event, it should works. It does not require to reload the pages.

Please show me a sample code :slightly_smiling_face:

The below code monitors your _User class in the database and give you an alert when new _User class is updated.

  let query = new Moralis.Query("_User");
  subscriptionAlerts = await query.subscribe();
  subscriptionAlerts.on("update", (object) => {
// you can add the your own code here which will run the an subscription alert is active.
alert("New User");
  });

How may use for getting push notifications?. Like if I send a request to a user then the concerned user should get an alert when online and in case of offline the alert should pop up on their next session. Similar to facebook request model.

I am not sure how that is done is facebook.
You can mimic it by adding a read column for a notification. So when a user received a notification through subscription alert, then you can mark the notification as read/viewed by adding the True or False in the read column.

So when the user gets back online you can Identify the notification which are read and not read by the user.
You can now query the database for unread notification and show them to the user.

1 Like

Thanks man it worked. Awesome :partying_face:

1 Like