Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ interface RedisStreamOptionsXreadGroup {
deleteMessagesAfterAck?: boolean;
}

export type RedisStreamOptions = RedisStreamOptionsXreadGroup;
interface RedisStreamOptionsXadd {
maxLen?: number;
}

export type RedisStreamOptions = RedisStreamOptionsXreadGroup & RedisStreamOptionsXadd;

// [id, [key, value, key, value]]
export type RawStreamMessage = [id: string, payload: string[]];
Expand Down
10 changes: 9 additions & 1 deletion lib/redis.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { RequestsMap } from './requests-map';
import { deserialize, generateCorrelationId, serialize } from './streams.utils';
import { RedisStreamContext } from './stream.context';
import { firstValueFrom, share } from 'rxjs';
import { RedisValue } from 'ioredis';

@Injectable()
export class RedisStreamClient extends ClientProxy {
Expand Down Expand Up @@ -104,9 +105,16 @@ export class RedisStreamClient extends ClientProxy {
try {
if (!this.client) throw new Error('Redis client instance not found.');

const commandArgs: RedisValue[] = [];
if(this.options.streams?.maxLen){
commandArgs.push("MAXLEN")
commandArgs.push("~")
commandArgs.push(this.options.streams.maxLen.toString())
}
commandArgs.push("*")
let response = await this.client.xadd(
stream,
'*',
...commandArgs,
...serializedPayloadArray,
);
return response;
Expand Down
15 changes: 14 additions & 1 deletion lib/redis.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CONNECT_EVENT, ERROR_EVENT } from '@nestjs/microservices/constants';
import { deserialize, serialize } from './streams.utils';
import { RedisStreamContext } from './stream.context';
import { Observable } from 'rxjs';
import { RedisValue } from 'ioredis';

export class RedisStreamStrategy
extends Server
Expand Down Expand Up @@ -140,7 +141,19 @@ export class RedisStreamStrategy

if (!this.client) throw new Error('Redis client instance not found.');

await this.client.xadd(responseObj.stream, '*', ...serializedEntries);
const commandArgs: RedisValue[] = [];
if(this.options.streams?.maxLen){
commandArgs.push("MAXLEN")
commandArgs.push("~")
commandArgs.push(this.options.streams.maxLen.toString())
}
commandArgs.push("*")

await this.client.xadd(
responseObj.stream,
...commandArgs,
...serializedEntries,
);
}),
);

Expand Down