Manish Kumar
Manish Kumar

Reputation: 10502

ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context

I am using jdish.publish in my web app and jedis.subscribe in my desktop app. so both are separate app.

I have this pubsub class

public class RedisNewPostListener extends JedisPubSub {

    private final Jedis jedis;
    private final AppInstances appInstances;

    public RedisNewPostListener(AppInstances instances, Jedis jedis) {
        this.jedis = jedis;
        appInstances = instances;
    }

    @Override
    public void onMessage(String channel, String message) {
        String[] pos = message.split("##");
        double lat = Double.parseDouble(pos[0]);
        double lon = Double.parseDouble(pos[1]);

        List<GeoRadiusResponse> members = jedis.georadius("UsersByLocation", lon, lat, GEO_SEARCH_RANGE, GeoUnit.KM);

i am calling it like

RedisNewPostListener postListener = new RedisNewPostListener(instances, jedis);
jedis.subscribe(postListener, "NewPostArrived");

I am getting this error:

redis.clients.jedis.exceptions.JedisDataException: ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context
    at redis.clients.jedis.Protocol.processError(Protocol.java:117)
    at redis.clients.jedis.Protocol.process(Protocol.java:151)
    at redis.clients.jedis.Protocol.read(Protocol.java:205)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:297)
    at redis.clients.jedis.Connection.getRawObjectMultiBulkReply(Connection.java:242)
    at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:248)
    at redis.clients.jedis.Jedis.georadius(Jedis.java:3452)
    at com.app.redis.RedisNewPostListener.onMessage(RedisNewPostListener.java:39)
    at redis.clients.jedis.JedisPubSub.process(JedisPubSub.java:129)
    at redis.clients.jedis.JedisPubSub.proceed(JedisPubSub.java:102)
    at redis.clients.jedis.Jedis.subscribe(Jedis.java:2628)

Upvotes: 32

Views: 20348

Answers (2)

zaid alkhateeb
zaid alkhateeb

Reputation: 11

In Redis, when a client subscribes to channels or patterns using commands like SUBSCRIBE or PSUBSCRIBE, the connection is put into a mode where only pub/sub commands (SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE, PUNSUBSCRIBE, PING, QUIT, RESET) are allowed. Regular commands like SET cannot be executed until the client unsubscribes from all channels.

Upvotes: 1

Conan Lee
Conan Lee

Reputation: 871

It seems that you use the same jedis client for subscribe and publish. You just need to create another client and one is for subscribe and the other is for publish

Upvotes: 63

Related Questions