Created
October 23, 2019 10:06
-
-
Save jgoday/e76deaf462989446b418d87d370b0dbb to your computer and use it in GitHub Desktop.
Postgres async notifications with tokio_postgres
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(poll_map)] | |
use futures::{stream, StreamExt}; | |
use futures::{FutureExt, TryStreamExt}; | |
use std::env; | |
use tokio::sync::mpsc; | |
use tokio_postgres::{connect, NoTls}; | |
#[tokio::main] | |
async fn main() { | |
let connection_parameters = env::var("DBURL").unwrap(); | |
let (client, mut conn) = connect(&connection_parameters, NoTls) | |
.await | |
.unwrap(); | |
let (tx, mut rx) = mpsc::unbounded_channel(); | |
let stream = stream::poll_fn(move |cx| conn.poll_message(cx).map_err(|e| panic!(e))); | |
let c = stream.forward(tx).map(|r| r.unwrap()); | |
tokio::spawn(c); | |
println!("After spawn listener"); | |
client.batch_execute("LISTEN test_notifications;").await.unwrap(); | |
loop { | |
let m = rx.recv().await; | |
println!("GOT MESSAGE"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/sfackler/rust-postgres/blob/3e4be865318ddd4a6b4493d689703db32ca3d184/tokio-postgres/tests/test/main.rs#L615-L648