Skip to content

feat(redis): accept an ioredis client or URL as the redis option - #301

Open
mcollina wants to merge 1 commit into
mainfrom
fix/59-redis-client
Open

mcollina wants to merge 1 commit into
mainfrom
fix/59-redis-client

Conversation

@mcollina

@mcollina mcollina commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Summary

The redis option only accepted RedisOptions, so the plugin always created and owned its Redis connection. Users who want to manage the connection themselves (their own error handling, connect/retry policy, or one client shared across the app) could not pass it in.

redis now accepts any of:

  • an ioredis Redis client: used as-is. The plugin never quits it and adds no error listener to it; the caller owns it. The message broker's pub/sub connections are derived from it with duplicate(), and the plugin closes those on app.close().
  • a Redis URL string, e.g. redis://localhost:6379/0: the plugin creates and closes the client.
  • RedisOptions: unchanged.

A client is detected with instanceof Redis, falling back to a method-shape check (duplicate, quit, disconnect, on, get, hset), so clients from a different copy of ioredis are recognised too (src/redis-client.ts).

Changes

  • src/index.ts: picks the client from the option and only quits a connection the plugin created.
  • src/types.ts: redis?: Redis | RedisOptions | string.
  • test/redis-client-option.test.ts: a provided client is used (session lands in Redis), stays ready after app.close(), gets no leftover listeners, and its duplicate()d connections end up closed. Also covers URL and options forms, plus the detection helper.
  • test-d/index.test-d.ts: type tests for the client and URL forms.
  • README: documents the three forms and who owns the connection. It also notes that onClose hooks run in reverse order, which matters if you quit your client from one.

Not breaking: existing RedisOptions configurations behave exactly as before.

Fixes #59
Supersedes #60

Thanks to @Codesleuth (David Wood) for the original implementation in #60. This PR takes the same approach, rewritten against current main after the redis option was retyped in #287. David is credited as co-author on the commit.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UF4S3w7iA83V9DVsasLJzM

The redis option only took RedisOptions, so the plugin always created and
owned its connection. Callers who want to manage the connection themselves
(e.g. attach their own 'error' handling, or share one client across the
app) had no way to pass it in.

The option now also accepts a Redis URL string and an ioredis Redis
client. A provided client is used as-is: the plugin never quits it and adds
no 'error' listener to it. The pub/sub connections the message broker
derives from it with duplicate() are still owned and closed by the plugin.
A client is detected with instanceof, falling back to a method-shape check
so clients from another copy of ioredis are recognised too.

Fixes #59

Co-authored-by: David Wood <david.p.wood@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UF4S3w7iA83V9DVsasLJzM

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide externally-managed Redis client instance

1 participant