I have a scenario in my application where I am listening to key expiry events in Redis.
I have created a subject and whenever there is an expiration event in redis, I push the expired key name in the expirationEventPool$
subject like this:
private async keepListeningForKeyExpiryEvents(): Promise<void> {
await this.redisEventListenerConnection.redis.pSubscribe("__keyevent@0__:expired", (message: string) => {
this.expirationEventPool$.next(message);
});
}
This happens right after the application bootstraps, now sometime later in the application lifecycle, if some method sets the expiry of the key (say after 10 seconds) and asks for the expiry observer, I return the observable from expirationEventPool$
like this:
public listenForEntityExpiration(entityName: string, entityId: string): Observable<string> {
return this.expirationEventPool$.pipe(
first((expiredEntityKey: string) => {
return expiredEntityKey === `${entityName}:${entityId}`;
}),
);
}
Now, the method who has asked for the entity expiration event, subscribes to it. After the expirationEventPool$
has emitted a value, it will get automatically unsubscribe because of the first
operator. My question is, can I resubscribe to the same expirationEventPool$
subject again? Or does it gets destroyed after the unsubscription?
Since expirationEventPool$
is a hot observable, yes you can resubscribe to it.