30

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)
Manish Kumar
  • 10,214
  • 25
  • 77
  • 147
  • Are you sure you are not using same JedisPool to get Jedis in both W-App and D-App? If yes, you need to close the jedis-client. – Vivek Sinha Nov 21 '17 at 09:33
  • “[Once the client enters the subscribed state it is not supposed to issue any other commands, except for additional `SUBSCRIBE`, `PSUBSCRIBE`, `UNSUBSCRIBE`, `PUNSUBSCRIBE`, `PING` and `QUIT` commands.](https://redis.io/commands/subscribe)” – Константин Ван Nov 21 '19 at 02:35
  • @КонстантинВан This is wierd since subscribed state is basically locking, so how is the client being reused? I am getting these errors after updating Jedis while it was working before. I see no way of how the connection is being returned to the pool while being in subscribe mode. – mjs Dec 11 '21 at 19:57

1 Answers1

55

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

Conan Lee
  • 691
  • 6
  • 10
  • 4
    I tripped in the same error message when using the redis npm client for a web app in a slightly different context. I had made sure to have only one client for publish and another for subscribe, but I was calling `redisClient.get()` and `redisClient.set()` methods in some rarely used and forgotten route using the subscribing client. So if you also end up here from google make sure your subscribe client isn't doing work unrelated to exclusively listening/unsubscribing/quitting. – arthropod Mar 11 '19 at 02:23
  • This answer is correct for Node.js Redis client as well. – Wong Jia Hau Oct 14 '20 at 03:51
  • Absurdly this is correct. Could not find this in any documentation anywhere. Wow. thanks – Spock Mar 07 '21 at 10:01