What would be the best approach for a real time inbox?

Hi community! I’m currently developing a site with node js and next ja for which I have created two collections (“users” and “messages”) so my question is the following:
What would be the best approach for creating a real time inbox?
It wouldn’t be like a real time chat. What I need is messages to appear in the inbox in real time and show a notification when a user sends a message to another.
I’m not sure if I should use a trigger for the “messages” collection, or change streams so I can monitor the collection in real time.
My problem is that I expect the site to have a lot of users using it simultaneously so I’m worried about how resource demanding this approach would be.
I’ve also tried using socket io to fire an event when a message is sent, but so far I haven’t been able to make the message get received in the frontend inbox component in real time, only by the users that the message goes to.
I would serious appreciate any guidance or advise as I’ve been struggling with this for several days now.
Thanks in advance

It’s very much related to how the frontend works. But if you have a long lived tcp connection between your client and the servers, either change stream events or a “push” can achieve this. A message to the recipient can be sent whenever the trigger is pulled.

Check https://www.youtube.com/watch?v=vvhC64hQZMk

i don’t know what this means.

Thanks a lot for your response.

I’m sorry I wasn’t very clear. What I meant to say is that I tried to emit an event with Socket.IO when a new message is sent, but I couldn’t make the new message be received only by the message recipient; instead, it was sent to all users.
Currently, I store all site messages in this collection with the fields “from_userID” and “to_userID.”
Would it be a good practice to either set a change stream event or a trigger so I can update the “messages” state in the frontend when a new message is received? Or would that be too resource demanding?

Thanks a lot for your help. It is greatly appreciated

i never use this, but it looks like you are broadcasting the events. Are you sure you have correctly written the code with their SDK? Their documentation should have explained how to use the APIs.

The answer will depend on your traffic, your resource provisioning, and many more. Actually for most of real time chats i have heared of, they all use sort of own push mechanism (e.g. websocket) instead of relying on database change stream. I think the key difference is whether you control everything or you delegate the trigger to your db tool. Maintaining change streams of course consume resources.

Probably using your own push logic is a bit easier, and given it’s 100% controlled by you, troubleshooting is also easier.

You might want to look at kafka for real time delivery of messages.

1 Like

Dear @farzana_kashif, you replied with SPAM in a thread I participated. What an error since I am a real and dedicated SPAM hater. I have started following you so that I catch your next attempt faster. I will not flag your post as SPAM yet. I want to expose you. I want people to know whom and what site to boycott.

Ok, @farzana_kashif, 3 days of SPAM exposure is enough. Your post has been flagged.