ValkeyStreamsPubSub
ValkeyStreamsPubSub is a PubSub and LeaseProvider implementation backed by Valkey Streams through Valkey GLIDE. It provides persistent cross-process delivery, consumer groups, redelivery, topic cleanup, and distributed leasing.
Use it for Valkey deployments. Use RedisStreamsPubSub for Redis deployments that use the official redis client. Both integrations implement the same Mastra behavior, with independent tests running against Valkey and Redis.
InstallationInstallation的直接链接
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/valkey-streams
pnpm add @mastra/valkey-streams
yarn add @mastra/valkey-streams
bun add @mastra/valkey-streams
Usage exampleUsage example的直接链接
import { Mastra } from '@mastra/core'
import { ValkeyStreamsPubSub } from '@mastra/valkey-streams'
export const mastra = new Mastra({
pubsub: new ValkeyStreamsPubSub({
url: 'valkey://localhost:6379',
}),
})
Constructor parametersConstructor parameters的直接链接
url?:
valkeyOptions.url. rediss:// and valkeys:// URLs enable TLS unless valkeyOptions.useTLS is set.valkeyOptions?:
keyPrefix?:
blockMs?:
maxStreamLength?:
streamIdleTtlMs?:
reclaimIntervalMs?:
reclaimIdleMs?:
maxDeliveryAttempts?:
Infinity to disable the cap.inFlightTimeoutMs?:
deliveryAttempt, so maxDeliveryAttempts still applies. Set this to recover hung handlers in single-consumer groups. 0 disables it.logger?:
Delivery behaviorDelivery behavior的直接链接
ValkeyStreamsPubSub supports pull delivery and doesn't support numeric replay offsets. Grouped subscribers compete for events through a shared consumer group. Subscribers without a group receive fan-out delivery through private consumer groups.
Use startFrom: "latest" to skip retained entries when a group is first created. The default, "earliest", reads retained entries first.
When a subscriber calls nack, the event is republished with an incremented deliveryAttempt and the original is acknowledged. Once an event reaches maxDeliveryAttempts, it's dropped instead of redelivered. Separately, each subscription periodically reclaims events that an earlier consumer in the group read but never acknowledged, controlled by reclaimIntervalMs and reclaimIdleMs.
A subscription never reclaims an event its own handler is still processing, so a slow handler isn't invoked twice for the same event. If a handler hangs, a different consumer in the group reclaims the event once it has been idle for reclaimIdleMs. In a single-consumer group there's no sibling to do that, so set inFlightTimeoutMs to have the subscription nack the event itself after that long.
Cleanup and shutdownCleanup and shutdown的直接链接
clearTopic(topic) deletes a topic stream and its consumer groups. flush() waits for in-flight publishes, and close() stops subscriptions and closes GLIDE connections.
await pubsub.clearTopic('workflow.events.run-123')
await pubsub.flush()
await pubsub.close()