Check out a free preview of the full The Good Parts of JavaScript and the Web course

The "Function Challenge 12" Lesson is part of the full, The Good Parts of JavaScript and the Web course featured in this preview video. Here's what you'd learn in this lesson:

In this challenge, you will write a function that makes a publish/subscribe object.

Preview
Close

Transcript from the "Function Challenge 12" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Douglas: So, this is a problem similar to the first one. We're going to make a function that makes a publish subscribe object, and it will reliably deliver all publications to all subscribers in the right order. So, the idea is that we might have multiple parties running in our system, and they need to be able to communicate with each other, and we will provide them a pub sub-instance that they will all share, that allows them to communicate.

[00:00:31]
But, we want to make sure that it works correctly so that it can reliably deliver all the messages to everybody in the right order. So, this is how it might work. We'll have our pubsub constructor, which will make a pubsub instance, which we then share with all the parties.

[00:00:48]
It will have a subscribe method on it which can receive a function, which will be called whenever somebody publishes, and they can publish by calling the publish method, which will then cause all of the subscribers to get called. Okay, so you can imagine how you might implement something like that it's not very complicated.

[00:01:09]
Here is a possible implementation. We've got a pub sub constructor, it has an array of subscribers where it's going to keep the full list of subscribers. It will return an object containing two methods. First the subscribe method will receive a subscriber and push it onto the subscribers list, and they publish method will Loop through all of the subscribers and call each one and give them the message.

[00:01:35]
Okay, so looks pretty straightforward, and unfortunately it doesn't do any of the things that we need to guarantee. It is possible to prevent people from publishing and subscribing. It's possible to,
>> Douglas: Remove subscribers and cause messages to get delivered out of order. Everything that this is supposed to prevent It will allow, so your job is to figure out how an attacker can do this.

[00:02:09]
So, if an attacker is one of the subscribers and wants to screw it up for all the other subscribers, how does he do that?
>> Speaker 2: He used the attack from before, and empty out the subscribers, right?
>> Douglas: That is something to be concerned about, we don't have a store method this time.

[00:02:29]
So, we don't provide a way of changing the push property of the subscribers array, but that's definitely something you want to be thinking about
>> Speaker 3: Whats the function now publishing? Sorry, if you push a function called publish, it's not because our response because it's crap. If you push a function that two weeks subscribers for example is like this because this.

[00:03:12]
When you get to me, the one I just pushed which is a function when you call subscribers eye. You call on publication, you now have access to this.
>> Douglas: You are exactly right. Unfortunately, Powerpoint compels me to force you to do the instead of particular order So please remember that we will come back to it okay?

[00:03:32]
>> [INAUDIBLE]
>> Douglas: Yeah, I'm a slave to PowerPoint here. Yeah, so we're gonna have to do that. So we're gonna have to, but I will call on you to do that again because you are absolutely right.
>> [INAUDIBLE]
>> Douglas: So let's start with how can I prevent other people from receiving messages.

[00:03:59]
You almost had it earlier.
>> Speaker 2: So a function, call it publish. Then overwrite it.
>> Douglas: No, it's much easier than that.
>> Speaker 4: Will this fail immediately on the next publish or will it not fail until the publish after the next publish?
>> Douglas: It should fail immediately
>> Douglas: You'll fail instantly.

[00:04:32]
>> Douglas: I'm sorry, it will fail on the next publication.
>> Speaker 4: I can just set publish,
>> Speaker 4: Can I just set publish equal to null?
>> Douglas: Yes, unfortunately Power Point compels me to ask you to remember that one too. I try to anticipate what order people to come up with these and

[00:04:54]
>> Speaker 4: I'm sorry.
>> Douglas: No, you're good.
>> Speaker 4: We're being tasked with sabotaging for many all those subscribers who have previously called subscribe from getting
>> Speaker 5: Publications when publishers is called. Okay.
>> Douglas: Okay. So since you're getting way ahead of me let me show the one that we need to do first.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now