Redis with Realtime

I’m trying to better understand how in-memory differs from using Redis with realtime, specifically subscriptions. Once I enable Redis in realtime.ts it establishes two connections to my redis instance. Right now the Redis subscribeClient and publishClient instances are a mystery to me.

I understand we speak to the server through channels with PubSub in our code, but even though I see realtime working and connections being made to my redis, I don’t see any actual key-value pairs. My understanding was that once you enable Redis, the publishClient enters the published data on the Redis database and then communicates it to the client with SSE. Then gets communicated with the subscribeClient. In my example I’ve implemented everything works, except there is no data in Redis. I’ve tried with two providers. Connected and checked using redis-cli but there are no keys, checking after each publish/subscribe.

Any clarity on this subject would be appreciated. If I am misunderstanding how Redis is used, I thank you for the clarification.

Redis is used as the store to keep track of who subscribes to a channel and the messages posted to that channel.

Unlike keys and values in that use the get and set commands in a traditional use of Redis, realtime/subscriptions use the subscribe and publish commands.

If you use a Redis client like Medis on OSX, there is a view

image

(it’s that radio wavy icon) ^^^

that shows the pubSub messages:

Here you can see that I have told Medis to monitor messages sent to the “newMessage:1” channel:

That’s because pubSub.subscribe('newMessage', roomId) sends to the channel newMessage and the room 1.

When I send a message via a mutation, it publishes and then the message appears in the monitoring view.

If I keep sending messages to that channel:

image

Here I am monitoring newMessage:100 but if I send to roomId 1, then I don’t see any:

image

But if I send to roomId 100, then I see it in my Medis viewer:

I probably should have documented how to use redis clients to monitor pubSub.

BTW live queries also uses pubSub to invalidate:

The channel is determine by:

  const key = `Auction:${auctionId}`
  context.liveQueryStore.invalidate(key)

Please ask more questions if I can help explain a bit better.

I really recommend using Medis to see the pubSub messages – it makes it much clearer I think.

1 Like

Thank you, that was very helpful. I noticed something by chance when building my app.

root-workspace-0b6124@workspace:. doesn't provide ioredis (p81cd2), requested by @redwoodjs/realtime.

If you want to use ioredis, do you have to add it to the root package.json first? I didn’t see that in the documentation.

The realtime setup commands should have installed this:

image

If it did not, then yes.

Yeah I thought it did, I have it listed in my api/package.json same as yours but yarn keeps telling me

root-workspace-0b6124@workspace:.
└─ @redwoodjs/realtime@npm:7.7.1 [dc3fc] (via ^5.3.2)
   └─ @graphql-yoga/redis-event-target@npm:3.0.0 [89a35] (via ^5.0.6)

✘ Package root-workspace-0b6124@workspace:. does not provide ioredis.

Is it expecting ioredis in the root package.json? :confused:

Indeed, adding this line to the root package.json fixes the issue.

  "dependencies": {
    "ioredis": "^5"
  }

Looking at my devDependencies it seems realtime snuck in here somehow:

“devDependencies”: {
@redwoodjs/auth-dbauth-setup”: “7.7.1”,
@redwoodjs/cli-storybook”: “7.7.1”,
@redwoodjs/core”: “7.7.1”,
@redwoodjs/project-config”: “7.7.1”,
@redwoodjs/realtime”: “7.7.1”,
@redwoodjs/studio”: “11”
},

I think it’s safe to say I can remove it from the root package.

1 Like

Correct. Just needs to be in the api package

1 Like