Get the latest msg from every group (like whatsapp)

Hello
I am trying to figure out how to get the latest chat message from every group
I have group table that has objectId and GroupMsges with 1:N relationships with Message Table.
The message table has ObjectId, From(User), To(User), Msg, and the created date

I want to get the latest message from every group that is destined to me. How can I achieve that.

Something like Get me the last message from every group where Message.To.objectId = ‘xyz’

Preferable if I can get the Group objectId along with the message details in one query. If not then just the message details is ok.

Can you advise how?
Thank you

Hi @snakeeyes

You can use: Backendless.Data.of("Message").find("GroupMsges[rel].name='group message name'")
https://backendless.com/docs/rest/data_inverted_relation_retrieval.html

Regards

Hi Inna,
Thank you but that’s not the intended behavioru I was asking about. I don’t know the group message name in this case. I want to get the latest message of “each” group in one query. Like Whatsapp chat.

Group 1: latest msg details
Group 2: latest msg details.

and so on
Thank you

You can try to use the grouping, where one of the condition is a max time for message.
The grouping itself can be made by the group name or group id which is message relates to.

Yess. This is what I want but how can do the grouping on Groups.objectId and the Max on related Msgs.Updated
They are different tables. Will I be querying the Groups table or Msgs table? Maybe you have a sample query for such case

I can propose you solution with two queries.
(1)
props=relGroup.name as groupName, Max(created) as maxTime
groupBy=groupName

(2)
props=objectId, relGroup.name as groupName, created
where=
You should create where clause, in which groupName and maxTime from the previous result are compared with the groupName and created from the current query.
something like
where=(groupName=‘g1’ and created=1234) OR (groupName=‘g2’ and created=3456)

So it has to be using 2 queries then. I was hoping to be one.
No worries. Thank you