Support Topics Documentation Slack YouTube Blog

Listen to user relation update

Hi,

I’m having trouble listening to relation updates on user objects.

What I want to do from a browser UI is to register a new user and assign it to a company, which is a separate table. I’ve made a 1:1 relation on the user to the company table. In order to create the relation I firstly create the user and then I create the relation with the setRelation function I get from the Backendless JS lib. However, this does not trigger an update event on the user object. If I change the name of a user I do get an update event.

This is my listener setup:

const userEventHandler = Backendless.Data.of(‘Users’).rt();

const onUserUpdate = user => {
  addColleague(user);
};

const onError = console.log;

userEventHandler.addUpdateListener(onUserUpdate, onError);

And this is my user creation setup:

function createUser() {
const newUser = new Backendless.User();
newUser.email = newUserEmail;
newUser.name = newUserName;
newUser.password = ‘pass’;

Backendless.UserService.register(newUser)
  .then(user => {
    Backendless.Data.of('Users')
      .setRelation(user, 'company', [currentUser.company])
      .then(() => {
        setShowAddModel(false);
        clearAddForm();
      })
      .catch(console.log);
  })
  .catch(console.log);

}

Is this the right way to do this and why is this not working?

Cheers,

Bennie

Hello @Bennie_van_der_Wel,

I have created internal ticket BKNDLSS-20460. We will discuss possibility of adding relation listeners

Oh, so you mean there is none what-so-ever. Does this mean changes for all kind of relations are not pushed? Or is it only for relations on the User object?

This then means that we cannot have a decent table architecture and have fully real-time data in our app. Do you have any work-arounds or tips on how to achieve this? How could I best handle my case?

How likely is it that you guys will implement this anytime soon?

Thanks for your answer!!

Bennie

Hi Bernie,

I would not say that not being able to track the state of the relations in real-time would prevent you from having a decent table architecture. With that premise all the solutions built on top of mysql, oracle and SQL server would need to be discarded… The Internet as we know it would be dead :slight_smile:

A workaround you could consider implementing is adding event handlers in the cloud code for the addRelation/setRelation events:

In your cloud code, you’d use the real-time messaging to notify the client-side that object’s relations had been modified.

Regards,
Mark

Hi Mark,

You are absolutely right. I was a bit annoyed that it didn’t work out-of-the box, my apologies :slight_smile:

I’m going to try your solution, thanks!!

Bennie

How would I go about this if the relation is added to a User object. If I select Users as Category, I dont have an addRelation event. Only if I choose Data Tables, but it seems Users do not fall in that Category.

Any tips?

I guess I could check the other way around. So make it so that a company has many users instead of a user had 1 company. What do you think?

Thanks, Bennie

Hi Bennie,

Users is just another table in the database, so go ahead and use Data, then you can scope the event to that table.

When you select Users as the category, you’re going down the path of creating an event handler for the Users-related APIs, such as login, registration, password recovery, etc.

Hope it helps.

Regards,
Mark

OK, will try. Thanks for the quick responses!!!

Hi Mark,

It works, thanks!.

However, it is quite some work to set this up for al the relation updates in several tables. The combination of setting relations on an object in a separate call then the creation call of that object and setting up custom pub/sub logic for all relation fields both in the frontend and in de backend(business logic of Backendless) to listen to those relation changes feels like a lot of work where I was kind of expecting that the out-of-the box real-time solution would cover such cases.

Do not get me wrong, I am glad I can make it work with this work-around, but I was wondering if relation updates would become something the RT db would cover in the near future.

Or am I still missing something?

Bennie

I basically now have two subsriptions:

  • Backendless.Data.of(‘Users’).rt(); for static data updates
  • Backendless.Messaging.subscribe(‘userSetRelationUpdates’); for relation updates

Is this the way Backendless intends it?

Hello @Bennie_van_der_Wel ,

We are pleased that the proposed solution has helped you. We have already created an internal ticket BKNDLSS-20460 and will discuss the possibility of adding relation listeners. Your subscriptions are currently a workaround to solve your problem.

Regards,
Inna

Hi Bennie,

The intent is to add support for real-time listeners for relation changes. When it is implemented, you will not need to do the messaging workaround.

The change you requested is already added to the road-map. To make it happen we will need to change the core app server to track the changes and modify all the RT-enabled SDKs (Android, iOS, JS, .NET) to include support for relation listeners. Additionally, we’ll need to modify the documentation to reflect the changes. While it seems like a simple change on the server, all other tasks do increase the scope.

We will let you know when the changes are in.

Regards,
Mark

Yes, I can imaging it is a lot of work. But it is great to hear it is on the roadmap :slight_smile:

Thanks for the support.

Bennie