redis4cats icon indicating copy to clipboard operation
redis4cats copied to clipboard

Publishing requires a separate connection

Open satorg opened this issue 4 months ago • 3 comments

It shouldn't – publishing can be made over a regular channel. Subscription does require a dedicated channel, but not publishing.

Currently, if one have two different applications – one publisher and one subscriber, both of them have to create a separate connection for that.

It is not usually a blocker, but is not exactly perfect either.

Moreover, Lettuce seems to allow publishing over a regular connection, therefore redis4cats could also support that, probably.

satorg avatar Aug 29 '25 06:08 satorg

One does not need 2 separate connections to both publish and subscribe Can you clarify , perhaps with some code ? TY

yisraelU avatar Sep 12 '25 16:09 yisraelU

@yisraelU , thank you for the reply! Perhaps, I missed something from docs and source code. So I'd appreciate if you could help me to figure it out.

I have the following case:

  • There are two different services (with different codebases).
  • The 1st service subscribes to some Redis channel and listens for messages and performs some actions on those messages.
  • The 2nd service publishes messages to that channel but it doesn't need to subscribe to any channel. Also the 2nd service executes some other Redis commands, like SET, GET, etc.

The problem here is that in order to execute PUBLISH I have to create a separate connection. In Redis4Cats I cannot re-use the same connection that I use for SET, GET, etc.

In Redis itself it is absolutely allowed – PUBLISH is no different from any other regular command. SUBSCRIBE is different – it converts a current channel into "subscription" one. But PUBLISH does not – it is just a regular command.

Speaking of code examples, let's take Quick Start from redis4cats docs:

  val run: IO[Unit] =
    Redis[IO].utf8("redis://localhost").use: redis =>
      for
        _ <- redis.set("foo", "123")
        x <- redis.get("foo")
        // _ <- redis.publish(someChannel, someMessage)
      yield ()

Here, redis.publish woudn't work because publish is defined in redis4cats-streams only and therefore requires a separate connection created with PubSub.mkPublishedConnection and then running its underlying Stream in order to make publishing working.

My point is that publish should not require any streaming involved. It can if necessary, but it doesn't have to.

Again, I might have missed something from the docs/sources and there is a way to achive publishing without streaming, but I couldn't find it.

satorg avatar Sep 12 '25 18:09 satorg

@satorg Ok , now I understand. Thats an artifact of the underlying client Lettuce which provides a StatefulRedisPubSubConnection instead of a StatefulRedisConnection that limits the interface to PubSubCommands. However there is no reason we cant support publish commands from the standard redis connection.

yisraelU avatar Sep 15 '25 19:09 yisraelU