hiredis C programming for XREADGROUP

197 views
Skip to first unread message

Rich

unread,
May 21, 2020, 2:59:36 AM5/21/20
to Redis DB
Hi All,
I am trying to use hiredis as C redis client to program Redis stream, when I use XREADGROUP as following:

reply = redisCommand(c, "XREADGROUP GROUP diameter Tom BLOCK 1 COUNT 1 NOACK STREAMS update_db >");

it always return a NULL pointer. If I replace ">" with others like "0", reply is not NULL. it looks ">" is causing the issue. Could someone please help me to figure out how to resolve this issue?

Thank you in advance!

Rich


Itamar Haber

unread,
May 21, 2020, 8:47:49 AM5/21/20
to redi...@googlegroups.com
Hi Rich,

Does the command return data when you run it with redis-cli?

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/redis-db/d46db2c1-7d96-4e2a-ac06-96d413d4d1a0%40googlegroups.com.


--

Itamar Haber
Technicalist Evangely

Phone: +972.54.567.9692

Redis Labs



Disclaimer

The information contained in this communication from the sender is confidential. It is intended solely for use by the recipient and others authorized to receive it. If you are not the recipient, you are hereby notified that any disclosure, copying, distribution or taking action in relation of the contents of this information is strictly prohibited and may be unlawful.

This email has been scanned for viruses and malware, and may have been automatically archived by Mimecast Ltd, an innovator in Software as a Service (SaaS) for business. Providing a safer and more useful place for your human generated data. Specializing in; Security, archiving and compliance. To find out more Click Here.

Rich

unread,
May 21, 2020, 12:50:27 PM5/21/20
to Redis DB
Hi ltamar Haber,
Thank you for your reply. I am afraid I cannot explain my tests clearly, I listed some some test cases.

TC 1: did not add data through redis-cli before I run my program.
        from redis-cli.
               127.0.0.1:6379> XREADGROUP GROUP diameter Tom BLOCK 1 COUNT 1 NOACK STREAMS update_db >
                (nil)
        from my program:
               reply = redisCommand(c, "XREADGROUP GROUP diameter Tom BLOCK 1 COUNT 1 NOACK STREAMS update_db >");
               reply is NULL

TC 2: add data through redis-cli before I run my program
        from redis-cli, i can get:
        127.0.0.1:6379> XREADGROUP GROUP diameter Tom BLOCK 1 COUNT 1 NOACK STREAMS update_db >
            1) 1) "update_db"
            2) 1) 1) "1590077941505-0"
                2) 1) "pri_key"
                2) "1006"
     
  (add data again because data was read by redis-cli)
   from my program:         
                     reply = redisCommand(c, "XREADGROUP GROUP diameter Tom BLOCK 1 COUNT 1 NOACK STREAMS update_db >");

                         size_t m = 0;
                         if (reply != NULL) {
                            for (m = 0; m < reply->elements; m++)
                            printf("XREADGROUP %s\n", reply->element[m]->str);
                       }

                  reply is not NULL but I did not get any data,  output is printed out but it is "null". it looks reply->element[m]->str contains null.

TC 3, change BLOCK to be 0, expect program to block until new data comes.
         from redis-cli, 
           the command:      "XREADGROUP GROUP diameter Tom BLOCK 0 COUNT 1 NOACK STREAMS update_db >" would block, when the new data comes, it will read data and return.
        
         However from program, I used: 
             reply = redisCommand(c, "XREADGROUP GROUP diameter Tom BLOCK 0 COUNT 1 NOACK STREAMS update_db >");           
         I am hoping it would block and wait for the new data (before running my program, I clean up the data by using flushall from redis-cli), but it actually did not block as expected, it return and reply is NULL.

Thank you,
Rich


On Thursday, May 21, 2020 at 8:47:49 AM UTC-4, Itamar Haber wrote:
Hi Rich,

Does the command return data when you run it with redis-cli?

On Thu, May 21, 2020 at 9:59 AM Rich <richar...@gmail.com> wrote:
Hi All,
I am trying to use hiredis as C redis client to program Redis stream, when I use XREADGROUP as following:

reply = redisCommand(c, "XREADGROUP GROUP diameter Tom BLOCK 1 COUNT 1 NOACK STREAMS update_db >");

it always return a NULL pointer. If I replace ">" with others like "0", reply is not NULL. it looks ">" is causing the issue. Could someone please help me to figure out how to resolve this issue?

Thank you in advance!

Rich


--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redi...@googlegroups.com.

Itamar Haber

unread,
May 21, 2020, 12:59:12 PM5/21/20
to redi...@googlegroups.com
> TC 3, change BLOCK to be 0 ...  However from program, I used...

Are you absolutely, positively and certainly sure that you ran the re-compile code in your test? Returning null indicates block's timeout, and my usual mistake is fixing the code but running an old artifact.

To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/redis-db/20d6b53d-5760-48e2-a589-9ffe00d2c651%40googlegroups.com.

Rich

unread,
May 21, 2020, 1:07:45 PM5/21/20
to Redis DB
yes, I just delete the binary and re-compile it. same result.  
reply = redisCommand(c, "XREADGROUP GROUP diameter Tom BLOCK 0 COUNT 1 NOACK STREAMS update_db >");  would not block and return with reply = NULL.

Thank you,
Rich

Itamar Haber

unread,
May 21, 2020, 1:15:46 PM5/21/20
to redi...@googlegroups.com
Weird - we must be both missing something obvious :)

I suggest you open an issue at https://github.com/redis/hiredis for help, with a minimal reproducible example.

To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/redis-db/7d5f7e17-e70a-440c-acd3-99f3afa0fec5%40googlegroups.com.

Rich

unread,
May 21, 2020, 1:18:01 PM5/21/20
to Redis DB
yes, here are the steps I verified.

1. make sure there is no data in redis, and then run "XREADGROUP GROUP diameter Tom BLOCK 0 COUNT 1 NOACK STREAMS update_db >"
   127.0.0.1:6379> XREADGROUP GROUP diameter Tom BLOCK 0 COUNT 1 NOACK STREAMS update_db >   ----> just wait here

2. from redis-cli to add data
    127.0.0.1:6379> XADD update_db * pri_key 1012
     "1590081349128-0"

3. check the result and that command return.

    127.0.0.1:6379> XREADGROUP GROUP diameter Tom BLOCK 0 COUNT 1 NOACK STREAMS update_db >
     1) 1) "update_db"
     2) 1) 1) "1590081349128-0"
         2) 1) "pri_key"
            2) "1012"
(185.48s)


Thank you,
Rich

Rich

unread,
May 21, 2020, 1:24:54 PM5/21/20
to Redis DB
sure, I will open a issue.
Thank you,
Rich

Jonathan Mendoza

unread,
May 21, 2020, 2:07:38 PM5/21/20
to redi...@googlegroups.com
Did you create the consumer group *after* the first message was sent?  If so, then the client wont consider that a *new* message that the consumer should read.

In other words:

XADD msg
XGROUP CEATE
XREADGROUP 

leads to Null

However:

XGROUP CEATE
XADD msg
XREADGROUP   

should pick the message up.


To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/redis-db/90d83b50-ba2c-4531-b3a7-af55ce933a6e%40googlegroups.com.

Rich

unread,
May 21, 2020, 2:39:12 PM5/21/20
to Redis DB
Hi Jonathan,
Thank you for your information.
I tried both way and the message still can not be picked up.  Here is my code pieces.

    printf("create a consumer group\n");
    reply = redisCommand(c, "XGROUP CREATE update_db diameter $ MKSTREAM");
    printf("XGROUP create %s\n", reply->str);
    freeReplyObject(reply);
 
    reply = redisCommand(c, "XADD update_db * %b %b", "pri_key",(size_t)7, "customer:1005", (size_t)13);
    if (reply != NULL) 
    {
        printf("XADD id is %s\n", reply->str);
    }
    freeReplyObject(reply);
    
    /*create consumer*/
    reply = redisCommand(c, "XREADGROUP GROUP diameter Tom BLOCK 0 COUNT 1 NOACK STREAMS update_db >");
    size_t m = 0;
    if (reply != NULL) {
    for (m = 0; m < reply->elements; m++)
        printf("XREADGROUP %s\n", reply->element[m]->str);
    }
    freeReplyObject(reply);

when I ran it, the result was:

create a consumer group
XGROUP create OK
XADD id is 1590085928884-0
XREADGROUP (null)

both XGROUP and XADD were successful but XREADGROUP failed to pick up message.

Thank you,
Rich
Reply all
Reply to author
Forward
0 new messages