Real Time Sync with Change Stream through Web API

Hello experts,

I have a question that I would like to ask and I’m hoping to get some answers that lead me in the right direction.

I’m developing a web page using ReactJS and MongoDB as my database. In the below sample drawing, I tried to explain how my architecture looks like.

I have an API deployed on AZURE that is responsible to handle all CRUD operations in MongoDB. I wanted to isolate my DB and Clients by creating this API as an additional layer. I’m currently able to perform all kinds of CRUD operations on my web page through FetchAPI.

The problem I’m facing now is real-time messaging between two users. I have a collection in my MongoDB called “UserMessages”. Every time when a user sends a message to another user, API creates a new document in this collection, and the only way to display this message right now is to call API (ex: GetUserMessages) constantly. I know this is not the way to go.

I was looking for ways to achieve real-time sync and found that Change Streams can help me but all the examples/tutorials I found works well when clients are directly connected to MongoDB. In my case, I have to connect my MongoDB through the API. Because I don’t want to provide DB connection information on the website.

I’d like to get your expertise to tell me how should I proceed to achieve my goal.

Thanks,

2 Likes

Did you figure this out? real time sync would be great.

Hi Rushi,

I decided to use SignalR in asp.net core. It helped me to solve my real-time messaging and notification issue. So the solution for me is kind of mixing SignalR and Change Stream together.