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

How to setup callback subscriber function to read messages ? #16

Closed
Steeew47 opened this issue Jul 17, 2019 · 4 comments
Closed

How to setup callback subscriber function to read messages ? #16

Steeew47 opened this issue Jul 17, 2019 · 4 comments

Comments

@Steeew47
Copy link

I'm struggle with subscribe feature. I don't know exactly how to set up callback function to (for example) print received messages. The connection settings are correct, because i can easily set value to key.

@sewenew
Copy link
Owner

sewenew commented Jul 17, 2019

First of all, you need to create a subscriber object:

auto redis = Redis("tcp://127.0.0.1:6379");
auto sub = redis.subscriber();

If you want to subscribe to a channel, i.e. SUBSCRIBE CHANNEL, you can set a callback with the Subscriber::on_message interface:

sub.on_message([](std::string channel, std::string msg) {
            cout << "received message: " << msg  << " from channel: " << channel << endl;
        });

If you want to subscribe to a pattern, i.e. PSUBSCRIBE PATTERN, you can set a callback with the Subscriber::on_pmessage interface:

sub.on_pmessage([](std::string pattern, std::string channel, std::string msg) {
            cout << "received message: " << msg << " from channel: " << channel << " of pattern: " << pattern << endl;
        });

After setting the callbacks, you can consume these messages in a loop:

while (true) {
    try {
        sub.consume();
    } catch (const Error &err) {
        // Handle exceptions.
    }
}

Also check the doc for other examples.

If you still have problem with this feature, you can show me your code. I'll try to point out your problem of the code.

Regards

@sewenew
Copy link
Owner

sewenew commented Jul 17, 2019

The above examples use Lambda as the callback function. If you're not familiar with lambda, you can also set a free function as the callback:

void print_message(std::string channel, std::string msg) {
    cout << "received message: " << msg  << " from channel: " << channel << endl;
}

void print_pmessage(std::string pattern, std::string channel, std::string msg) {
    cout << "received message: " << msg << " from channel: " << channel << " of pattern: " << pattern << endl;
}

sub.on_message(print_message);
sub.on_pmessage(print_pmessage);

while (true) {
    try {
        sub.consume();
    } catch (const Error &e) {
        // Handle exception
   }
}

@Steeew47
Copy link
Author

As you guessed i'm not familiar with a Lamba, but your free function as the callback works as intended. I am very grateful for quick response. We can close issue.

@sewenew
Copy link
Owner

sewenew commented Jul 17, 2019

I'm glad that the problem is solved. If you have any other problems with redis-plus-plus in the future, always feel free to let me know.

If you like this client, also feel free to star it :)

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

No branches or pull requests

2 participants