'Continuously listen to Redis stream using xreadgroup - redis-plus-plus client
I am trying write a C++ code for a consumergroup to continuously listen to the server and reads whenever a new message is added to the stream. The BLOCK parameter would block only until message is unavailable. It is required to send XREADGROUP command again in order to listen to the last added message or a new message. How can consumergroup be made online/active all the time and make the program to not end and keep listening to the stream? My code:
#include <iostream>
#include <sw/redis++/redis++.h>
#include <unordered_map>
using namespace sw::redis;
int main(){
auto redis = Redis("tcp://127.0.0.1:6379"); //connect to redis server
using Attrs = std::vector<std::pair<std::string, std::string>>;
Attrs attrs = { {"f1", "v1"}, {"f2", "v2"} }; //message key-value pair
using Item = std::pair<std::string, Optional<Attrs>>;
using ItemStream = std::vector<Item>;
std::unordered_map<std::string,ItemStream > result;
auto id = redis.xadd("Mystream", "*",attrs.begin(), attrs.end()); //add message to stream "Mystream"
redis.xgroup_create("Mystream", "Mygroup", "$"); //create consumer group - Mygroup
redis.xreadgroup("Mygroup", "consumer1", "Mystream",">" ,1, std::inserter(result, result.end())); // read last added message by group: Mygroup, consumer: consumer 1 from stream: mystream
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
