pub-sub

222 views
Skip to first unread message

khi...@googlemail.com

unread,
Jan 23, 2016, 4:23:58 PM1/23/16
to lettuce-redis-client-users
hi there,
what is the correct way to have a connection which can both publish & listen to messages ? do i need to create 2 StatefulRedisPubSubConnection connection ? (1 for listening & 1 for publishing)

this code doesnt work for me , i got
"com.lambdaworks.redis.RedisCommandExecutionException: ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context" when try to publish.

i'm on Lettuce 4.0.2
thanx


RedisClient client = RedisClient.create(server);
//connect
StatefulRedisPubSubConnection<String, String>  listener = client.connectPubSub();
//listener
listener.addListener(new RedisPubSubAdapter<String, String>(){
            @Override
            public void message(String channel, String message) {
                System.out.println("got msg:" + message + " on channel " + channel);
            }     
        });

//subscribe
listener.async().subscribe(addr);

//send msg
listener.sync().publish(addr, msg);

Mark Paluch

unread,
Jan 24, 2016, 5:48:15 AM1/24/16
to lettuce-redis-client-users, khi...@googlemail.com
For publishing and subscribing you need two connections. This is a Redis requirement.

Try the following code:


RedisClient client = RedisClient.create(server);
//connect
StatefulRedisPubSubConnection<String, String>  listener = client.connectPubSub();
//listener
listener
.addListener(new RedisPubSubAdapter<String, String>(){
           
@Override
           
public void message(String channel, String message) {
               
System.out.println("got msg:" + message + " on channel " + channel);
           
}      
       
});


//subscribe
listener
.async().subscribe(addr);


StatefulRedisConnection<String, String>  publisher = client.connect();
//send msg
publisher
.sync().publish(addr, msg);

khi...@googlemail.com

unread,
Jan 24, 2016, 8:12:18 AM1/24/16
to lettuce-redis-client-users, khi...@googlemail.com
thanx, that works.
Reply all
Reply to author
Forward
0 new messages