I am trying to write a client which will listen for events from PostgreSQL in a loop. Below is something I could come up with (considering I am beginner in rust)
pub fn future(&self) -> Result<(), ()> {
let url = "postgresql://root:root@127.0.0.1/test";
Connection::connect(url, TlsMode::None)
.map_err(|error| {
println!("{:?}", error);
})
.and_then(|conn: Connection| {
conn.execute("LISTEN modify_event", &[])
.expect("Could not send LISTEN");
Ok(conn)
})
.and_then(|conn: Connection| {
let notifications = conn.notifications();
let mut blocking_iter = notifications.blocking_iter();
println!("Waiting for notifications...");
blocking_iter
.next()
.map(|data| match data {
Some(data) => serde_json::from_str::<User>(&data.payload)
.map_err(|error| {
println!("Postgre data deserialization error: {}", error);
})
.map(|subscriber| {
println!("{:?}", subscriber);
})
.unwrap(),
_ => (),
})
.map_err(|error| {
println!("{:?}", error);
})
})
}
And in some other module, I would spawn this future.
However, I am not able to figure out how to run this listener in loop. Also, could anyone please provide me a simple example to do the same with tokio runtime in tokio-postgres (considering `handle` parameter for Connection in tokio-postgres · Issue #418 · tokio-rs/tokio · GitHub).