Subject: Both an observable and an observer — it can receive values (next()) and emit them to all subscribers. Used as a bridge between non-reactive and reactive code. In reactive programming, a Subject is both an Observable and an Observer. You can push values into it with next(), and every subscriber receives them.
That makes it a bridge between ordinary code and reactive streams. A button handler or a WebSocket callback can call subject.next(value), and the rest of the application listens to the stream.
What should you keep in mind with Subject?
RxJS offers several variants. A BehaviorSubject remembers the latest value and gives it to new subscribers immediately, which is handy for current state like the logged-in user. A ReplaySubject replays several past values. Subjects are easy to overuse; exposing them publicly lets any code push values. A common pattern is to keep the subject private and expose only the read-only observable to the rest of the code.
Implement proven backend architecture patterns including Clean Architecture, Hexagonal Architecture, and Domain-Driven Design. Use this skill when designing clean architecture for a new microservice, when refactoring a monolith to use bounded contexts, when implementing hexagonal or onion architecture patterns, or when debugging dependency cycles between application layers.
Implement Command Query Responsibility Segregation for scalable architectures. Use when separating read and write models, optimizing query performance, or building event-sourced systems.