0

In my chat-like application, I'm using the ChangeStream feature in my Rust-MongoDB code to retrieve new posts made by other users in a specific channel/group(chat of multiple users). I'm using websockets to send these new posts in real-time to connected clients. However, I'm facing an issue where the websocket doesn't receive messages when a new post is created. The watch() method does not trigger for insert operations if i establish my pipeline for only matching channel, it works fine if i leave the pipeline making it just None, but in this case watch() will trigger for all operations, but i want to trigger for specific channel. The snippet:

let pipeline = vec![doc! {
    "$match": {
        "channel_id": channel_id
    }
}];

// Listen for changes in posts
let change_stream = state
    .db
    .posts_collection
    .watch(pipeline, None)
    .await
    .map_err(|err| {
        eprintln!("Error creating change stream: {:?}", err);
        "Failed to create change stream".to_string()
    });

My question is: How can I modify the code to ensure that the watch() method triggers for insert operations(other operations too) in the specified channel when I establish a pipeline for the matching channel? I would greatly appreciate any guidance or suggestions to resolve this issue.

0 Answers0