Check out a free preview of the full Enterprise Architecture Patterns course:
The "Communication with Subjects" Lesson is part of the full, Enterprise Architecture Patterns course featured in this preview video. Here's what you'd learn in this lesson:

Lukas explains how communication messages are sent and received between subjects in an application. A subject's asObservable method returns a stream and any observer subscribed to the stream will receive updates when an event is emitted.

Get Unlimited Access Now

Transcript from the "Communication with Subjects" Lesson

[00:00:00]
>> Streams are very good at communicating events and communicating that, hey, this is what happened, it happened. And this is what you need to know about it. In our examples or rather our concepts Folder. If you go down to the bottom, and you look for event communication, we have a form and a button.

[00:00:32] And when we click it, we're calling notify. And we're calling notify sending the message in and then we're calling dispatch on this notification service. So if I click in here, what do we have? A subject. A subject is essentially a mechanism that produces an observable stream, but also produces the mechanisms.

[00:01:00] To control, that observable stream. And so, within our notification service, and I do use this. That, we're creating a subject, and then, we are exposing it, as an observable. So, somebody can come and say, I want to subscribe to this. But then we also have the ability to dispatch events or notifications.

[00:01:29] And when we call this dot subject.next, the observable stream underneath of it will essentially push that down the line. So if I go to I believe it's app component. Let me see here. All right. So if you look at Angie Annette We are, subscribing to the notifications. And anytime a notification comes in the pipeline.

[00:02:06] So, this is the initial output. This is the input. It's just passing off that notification, to this particular method right here. And it's servicing it. So this is a very simple way. If we look at the notification service, if you want to create an event bus to pass information around your application, and you don't need to store.

[00:02:32] It's just temporal information that you just it's there and it's gone. We can do that in, I don't know, six, seven lines of code. And you have the ability from one component to trigger and say, hey, I need you to notify cause the notification service and handles it.

[00:02:55] And then over here, you can subscribe to it. And so, in this case, this is where we start to see the observer pattern. Kinda take, the forefront. So in event communication, we can put anything in here. Holla at your boy, and I'm gonna go notify now this is from a child component that's then delegating backup to the notification service.

[00:03:26] And you can see it down here. So I was able to implement this mechanism. In 1015 lines of code, and I can move this around and so this is a very good way to communicate events and state across your application. For instance, what do we think? And GRS store is using to communicate state changes when the store changes.

[00:03:56] How do we know? What is like what's communicating under the hood? How's that? How does that work? Well, it's a behavior subject. That's it. You are literally your entire application state disease single state tree that's in an observable that anytime it changes, the consumers are being notified. So, there's a lot of other I think really cool things here that when you have the time, I really encourage you to go through, look at that.

[00:04:28] Even look at the slides and the repo and start to think about how you can actually take some of these things and build real applications. So for instance, a real time sports counter or a slideshow. This is super easy. So when we did the left right, well do a slideshow.

[00:04:52] You could even do a spaceship game. So this is a really kind of a wacky like, it's not very full feature. But interesting enough, I think, pragmatic programmer has a pretty good book on building RSGS apps and this is one of the things that they do is the ability to make a very simple shooter.

[00:05:13] Is anybody ever like went running and just because they're narcissists? They want to know like how far did I go? Only because I could brag to my friends. Well here you can say like I want to click here. I came flying in from way over here or whatever.

[00:05:28] So this is a really easy way. That's all we're doing now is we're mapping the stream of the mouse clicks. Another one. Where are you? I'm right here. What about over here? You're just capturing those events and responding to it. So I would say definitely go through the concepts poker on the slides poker on the code.

[00:05:51] And then in the examples section, you can see some ways that you could actually use this to do something meaningful or some kind of a unit of work.