Creating Realtime Chat using MessageKit and MongoDB swift 5

I am trying to implement a realtime chat into my iOS application. I am using MessageKit and following this tutorial:

The tutorial is fore firebase but I am trying to convert it to work with my MongoDb backend as best I can. One of the issues I am facing is trying to replicate the following code:

self.docReference = doc.reference
//fetch it's thread collection
.order(by: "created", descending: false)
.addSnapshotListener(includeMetadataChanges: true, listener: { (threadQuery, error) in
if let error = error {
print("Error: \(error)")
} else {
for message in threadQuery!.documents {
let msg = Message(dictionary:
print("Data: \(msg?.content ?? "No message found")")

I am unsure how I would make a listener for changes to a collection thread? The way my app works is it makes Http Requests and I have a running server that makes the actual mongo queries, so I need a way to setup a listener that works with that. Thanks for all the help!


This is the kind of schema I am trying to emulate, the examples is in Firebase, the only part I am having trouble figuring out is how theres a collection nested within a document? How would I go about doing that in Mongo enter image description here