Update dashboard elements of plotly-DASH using pymongo.change_streams()

I am trying to build a customized dashboard using plotly-dash which needs to get updated (automatically) whenever a new record is inserted into the MongoDB Atlas collection on the cloud. I was looking for examples online but could not find any. For example, lets assume the data that gets inserted into mongoDB (asynchronously at random time intervals) is a time series data like:

"time" : "14.04.2022 21:50:59",
"balance" : 10500.50

I understand that it is possible to set an interval to routinely update charts in dash, but is it possible to use pymongo.change_streams to watch for new insertions into the collection, and then automatically update the plotly charts or dataframe table in the dashboard.

Can anyone show a simple example of a plotly-dash line chart or may be a datatable that gets updated using plotly callbacks and pymongo.change_streams whenever a new record is inserted into a MongoDB collection?

Best Regards,

One possible option would be to create a small data routing server (e.g. using Quart) that integrates with pymongo and exposes a websocket endpoint with the data, which is then read by a Websocket component in Dash.

Thanks but seems slightly roundabout task, Perhaps it should be possible with pymongo.change_streams itself. It seems someone has asked a similar question in stackoverflow but no reply from any plotly/mongoDB expert on this issue, so far?

To my knowledge, there is no native integration between Dash and pymongo streams. That’s why I suggested to use a web socket server as an adapter (unlike using e.g. an interval component, the web socket solution would in fact yield instant updates).

If you need a native integration, I believe the best approach would be to create a custom Dash component that takes care of the integration. If you are not too afraid of JavaScript/React it should be doable, but it’s a bit more work that the adapter solution, I would say.


Greetings Emil,
I followed this tutorial to setup websockets with MongoDB, and this is the output I get of this working websocket connection to MongoDB
https://cdn.discordapp.com/attachments/931545568931631194/983232389763448852/unknown.png .

I am trying to build a plotly-dash app that interfaces with a MongoDB collection where data is continuously written at asynchronous time intervals. The dash app needs to plot the equity field on the y-axis and time on the x-axis and also update the live streaming data on dash cards. But I don’t know how to connect this MongoDB ChangeStream websocket to Dash. Should I use quart or quart-motor package or the existing code in this mongodb tutorial would suffice with some minor modifications?

The Dash websocket tutorials sometimes uses JS code, which I don’t understand:
1.) Websocket
2.) Build a realtime Dash App with websockets | by Jacky Shi | Medium

Would be grateful for any guidance or small working example, as information on the internet to setup websocket connection between mongoDB and Dash is very scarce?

Best Regards,