« Use DynaPDF to create… | Home | Three months until Cl… »

Watch MongoDB Database changes in Xojo

Did you know that MongoDB has a watch feature (change stream) to see what happens in the database?

You can watch a client (your connection) with Watch method in MongoCollectionMBS, MongoClientMBS or MongoDatabaseMBS classes. Once watch is active, you can query NextChange method on the MongoChangeStreamMBS object you got from Watch regularly and see if you get a JSON with a new change. In our example database, we run a Timer to look for new changes every second and this shows the change as new record in our example. You can use this feature for various purposes, including:

  1. Real-time Data Synchronization: You can use watch feature to keep data in sync across different parts of your application or between different services. When a change occurs in a MongoDB collection, you can capture that change and react accordingly to keep your data up-to-date, e.g. copy values to another SQL database.
  2. Notification Systems: You can implement real-time notification systems, such as sending alerts, emails, or messages to users or applications when specific changes or events happen in the database. This is useful for building chat applications, social networks, or any application that requires real-time updates.
  3. Data Replication: Change Streams can be used to replicate data between MongoDB instances or databases. For example, you can replicate data from a primary MongoDB server to one or more secondary servers for fault tolerance and load distribution.
  4. Auditing and Logging: You can track and log changes made to your data for auditing purposes. This can help you maintain a record of who made changes, when they were made, and what the changes were.
  5. Triggers and Workflow Automation: You can use Change Streams to trigger specific actions or workflows when certain events occur in your database. For example, you can automatically update related documents or trigger external services in response to changes.
  6. Data Analytics and Reporting: Real-time data feeds from Change Streams can be used for building analytics dashboards and generating real-time reports based on the changes happening in your MongoDB database.
  7. Caching: You can use Change Streams to update cache data whenever there are changes in the database. This can help improve the performance of your applications by serving frequently accessed data from cache.
  8. Building Live Feeds: If you're developing applications that require live feeds or activity streams (e.g., social media timelines or news feeds), Change Streams can help you efficiently implement these features.
  9. Data Transformation: You can use Change Streams to trigger data transformation processes, ensuring that your data remains consistent and conforms to the desired format.
  10. Reactive Programming: Change Streams can be integrated into reactive programming frameworks or libraries to create responsive and event-driven applications.

MongoDB Change Streams provide a flexible and powerful way to react to database changes in real-time, making them a valuable tool for a wide range of applications that require real-time data processing and synchronization. 

 

Back in Xojo you would open the database with Database method in MongoClientMBS class and then start watching changes in that database with Watch method and get the MongoChangeStreamMBS object and then call NextChange regularly to pick up new data. That may be a method just checking every a few milliseconds in a service application in the background to process incoming changes. Or like in the example below use a Timer to evaluate an expression every second to check for changes and if a change happened, process it e.g. by showing in a listbox


Sub Open() // open this server Dim u As New MongoURIMBS("mongodb://localhost/") client = New MongoClientMBS(u) // open this database Database = Client.Database("test") // now watch for changes ChangeStream = Database.Watch("{}") End Sub

The action event of the timer will store the results in a ListBox and the user can see them, when you click on the entry:


Sub Action() If ChangeStream <> Nil Then Dim json As String If ChangeStream.NextChange(json) Then dim d as new date List.AddRow d.SQLDateTime, json End If End If End Sub

Please try it and enjoy the new feature for 23.5 plugin version of MBS Xojo MongoDB Plugin.

05 11 23 - 10:56