Closed NanthiniRasu closed 1 year ago
The problem is that the corresponding Subscriber
object, i.e. sub
, is destroyed once redis_async_init
returns. Once Subscriber
is destroyed, e.g. out of scope, the underlying connection will be closed, and you cannot receive any message.
In order to solve the problem, you need to define Subscriber
as a global object or save it some where, e.g. return it from redis_async_init
, and keep it alive during subscribing messages.
B.T.W. I don't see a timer in your code. Not quite sure what's it means in your question.
Regards
This below code we have integrated to our main process. if we comment out the sleep or if timer expires redis is not receiving message from subscriber.
include
include
include
include "sw/redis++/async_redis++.h"
include "nsm_data.h"
using namespace std; using namespace sw::redis;
extern "C" { void redis_async_init(); void convert_path_to_table_id_and_fetch_key(char *path); };
void get_redis_path(std::string channel, std::string msg) { cout << "Received message" << msg <<endl; const char path = msg.c_str(); convert_path_to_table_id_and_fetch_key((char )path); }
void redis_async_init () { ConnectionOptions opts; opts.host = "127.0.0.1"; opts.port = 6379;
ConnectionPoolOptions pool_opts; pool_opts.size = 3;
auto event_loop = std::make_shared();
auto async_redis = AsyncRedis(opts, pool_opts, event_loop);
auto sub = async_redis.subscriber();
Future ping_res = async_redis.ping();
cout << ping_res.get() << endl;
Future fut1 = sub.subscribe("mychannel");
sub.on_message(get_redis_path);
sleep(60); }
Our understanding from doc is asyncredis will receive the message once we registers the callback.But it's not receiving the messages once it's exit from init api.
Let us know what we are missing or please share us some example for proceeding with asyncredis connection