Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of redis subscribe/psubscribe #258

Open
GopherJ opened this issue Feb 17, 2020 · 7 comments
Open

Example of redis subscribe/psubscribe #258

GopherJ opened this issue Feb 17, 2020 · 7 comments

Comments

@GopherJ
Copy link

GopherJ commented Feb 17, 2020

Hello guys, is there a way to use redis subscribe/psubscribe in actix + actix-web system?

I don't want to create a seperate thread for handling this, is there a better way to do this?

@GopherJ
Copy link
Author

GopherJ commented Feb 17, 2020

See also: actix/actix-redis#20

@pymongo
Copy link
Contributor

pymongo commented May 1, 2020

@GopherJ actix-redis publish example

use actix::Addr;
use redis_async::resp_array;
use actix_redis::{Command, RedisActor};
use actix_web::{middleware, web, App, HttpResponse, HttpServer};

#[actix_web::get("/")]
async fn cache_stuff(
  redis: web::Data<Addr<RedisActor>>
) -> HttpResponse {
  let datetime_now: String = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
  let _future = redis.send(Command(resp_array!["PUBLISH", "test", datetime_now.as_str()])).await;
  HttpResponse::Ok().body(format!("successfully publish {message} to `test` channel", message = datetime_now))
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
  std::env::set_var("RUST_LOG", "actix_web=trace,actix_redis=trace");
  env_logger::init();

  HttpServer::new(|| {
    App::new()
      .data(RedisActor::start("127.0.0.1:6379"))
      .wrap(middleware::Logger::default())
      .service(cache_stuff)
  })
    .bind("0.0.0.0:8080")?
    .run()
    .await
}

[dependencies]
actix = "*"
actix-rt = "*"
actix-web = "*"
actix-redis = "*"
redis-async = "*"
env_logger = "*"

@GopherJ
Copy link
Author

GopherJ commented May 1, 2020

@pymongo hello, what's really needed is subscribe and psubscribe

@pymongo
Copy link
Contributor

pymongo commented May 4, 2020

@GopherJ
I guess there is no redis sub/pub API in actix-redis.

actix-redis depend on redis-async,

you can check out pubsub example in redis-async/examples/pubsub.rs

@GopherJ
Copy link
Author

GopherJ commented May 4, 2020

@pymongo I already checked them before creating this issue and that's also why I created this issue, because we should have a way to subscribe/publish.

@shanliu
Copy link

shanliu commented May 2, 2022

I also encounter this problem. Is there a plan to improve it?
@robjtede

@shanliu
Copy link

shanliu commented May 2, 2022

@GopherJ
Have you solved this problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants