Re: SUBSCRIBE TO A CHANNEL USING C LANGUAGE.

245 views
Skip to first unread message

Salvatore Sanfilippo

unread,
Nov 28, 2012, 5:01:16 AM11/28/12
to Redis DB
You can check how I do it in the "sentinel.c" code in the Redis
distribution, but I agree that this should be more clearly documented.

In short, to subscribe to "ILOVEUPPERCASE" channel:

void myCallBackFunction(redisAsyncContext *c, void *reply, void *privdata) {
redisReply *r;
if (r == NULL) return;

/* Sanity check in the reply we expect, so that the code that follows
* can avoid to check for details. */
if (r->type != REDIS_REPLY_ARRAY ||
r->elements != 3 ||
r->element[0]->type != REDIS_REPLY_STRING ||
r->element[1]->type != REDIS_REPLY_STRING ||
r->element[2]->type != REDIS_REPLY_STRING ||
strcmp(r->element[0]->str,"message") != 0) return;

printf("%s\n",r->element[2]->str);
}

retval = redisAsyncCommand(context, myCallBackFunction, NULL,
"SUBSCRIBE %s", "ILOVEUPPERCASE");


Cheers,
Salvatore

On Wed, Nov 28, 2012 at 10:30 AM, MSP <mohitb...@gmail.com> wrote:
> I want to listen for messages continuously on a channel (PUB/SUB Redis)
> using C launguage . I am using hiredis library.
> Can anyone suggest the way to achieve this?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Redis DB" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/redis-db/-/0ZyiVsVwet0J.
> To post to this group, send email to redi...@googlegroups.com.
> To unsubscribe from this group, send email to
> redis-db+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/redis-db?hl=en.



--
Salvatore 'antirez' Sanfilippo
open source developer - VMware
http://invece.org

Beauty is more important in computing than anywhere else in technology
because software is so complicated. Beauty is the ultimate defence
against complexity.
— David Gelernter

MSP

unread,
Nov 29, 2012, 4:30:36 AM11/29/12
to redi...@googlegroups.com
Thank you so much...done with my problem statement now.

But one more thing.....

I have written a client in c that listen on particular channel say "CHNL101" and i have a publisher written in c which publishes the message to the same channel.
In this scenario client successfully receives the messages.

But when i am publishing through redis-cli....client does not recieve anything. WHY???
i am publishing like this
redis 127.0.0.1:6379> publish chnl mohit
(integer) 0

Salvatore Sanfilippo

unread,
Nov 29, 2012, 4:33:14 AM11/29/12
to Redis DB
On Thu, Nov 29, 2012 at 10:30 AM, MSP <mohitb...@gmail.com> wrote:
> redis 127.0.0.1:6379> publish chnl mohit
> (integer) 0

The hint is in the "(integer) 0".

There are no subscribers for that channel according to Redis.

Cheers,
Salvatore

MSP

unread,
Nov 29, 2012, 4:38:48 AM11/29/12
to redi...@googlegroups.com
Got it....CHNL and chnl are different channels.
So channel names are case-sensitive unlike redis commands...

Salvatore Sanfilippo

unread,
Nov 29, 2012, 4:40:06 AM11/29/12
to Redis DB
On Thu, Nov 29, 2012 at 10:38 AM, MSP <mohitb...@gmail.com> wrote:
> So channel names are case-sensitive unlike redis commands...

Case sensitive as key names indeed.

MSP

unread,
Nov 29, 2012, 9:31:01 AM11/29/12
to redi...@googlegroups.com
Hi,

According to hiredis documentation : "When redisAsyncCommand  function is called, the connection is not immediately terminated. Instead, new commands are no longer accepted and the connection is only terminated when all pending commands have been written to the socket, their respective replies have been read and their respective
callbacks have been executed."


Core is generated by following piece of code....

for(i=0;i<5000;i++)
        redisAsyncCommand(c, getCallback, NULL, "get num");
    redisAsyncDisconnect(c);

publisher: async.c:289: __redisAsyncDisconnect: Assertion `__redisShiftCallback(&ac->replies,((void *)0)) == -1' failed.
Aborted (core dumped)

For values of i upto 500 ,this code  is working fine

MSP

unread,
Nov 30, 2012, 8:47:46 AM11/30/12
to redi...@googlegroups.com
Hi...

retval = redisAsyncCommand(context, myCallBackFunction, NULL, 
"SUBSCRIBE %s", "ILOVEUPPERCASE");  

After this my program starts listening on the channel for messages infinitely.....

can you suggest how to return from this call ? 

This piece of code is NOT behaving correctly
    redisAsyncCommand(c,myCallback, NULL, "SUBSCRIBE CHNL");
    redisAsyncDisconnect(c);
i mean redisAsyncDisconnect(c) will wait for the callback to return....
 
Plz throw some light on how to return from subscribe command. 
Reply all
Reply to author
Forward
0 new messages