1 00:00:01,310 --> 00:00:06,410 At this point our little test application has exactly one publisher and one listener inside of it. 2 00:00:06,410 --> 00:00:10,970 Now let's imagine that we eventually deploy this test application into our Cuban cities cluster and 3 00:00:10,970 --> 00:00:16,340 maybe we use it for a real application at some point in time we might realize that the order service 4 00:00:16,370 --> 00:00:21,440 is getting a lot of traffic and so we might decide to scale up the order service in some way. 5 00:00:21,440 --> 00:00:26,360 We can either scale the order service vertically by giving the order service some more powerful CPE 6 00:00:26,360 --> 00:00:28,640 you more ram and so on. 7 00:00:28,640 --> 00:00:34,010 Or alternatively we could try to scale it horizontally by just creating a second instance of the order 8 00:00:34,010 --> 00:00:40,370 service so we might have two copies of the exact same service and in all regards they're identical in 9 00:00:40,370 --> 00:00:41,660 nature. 10 00:00:41,660 --> 00:00:45,920 Now of course we would want to make sure that both these services would be able to connect to our Nat 11 00:00:45,920 --> 00:00:47,400 streaming server. 12 00:00:47,420 --> 00:00:50,760 Let's try opening up our terminal once again inside the terminal. 13 00:00:50,780 --> 00:00:52,250 Let's create a second window. 14 00:00:52,280 --> 00:00:56,690 I guess the third window in this case and tried running another copy of the order service and just see 15 00:00:56,690 --> 00:00:58,350 what happens. 16 00:00:58,440 --> 00:00:58,700 All right. 17 00:00:58,710 --> 00:01:00,180 So back inside my terminal. 18 00:01:00,360 --> 00:01:02,200 If you are running I turned 2. 19 00:01:02,220 --> 00:01:07,260 You can select a window that's running the listener and then do a command shift D. 20 00:01:07,290 --> 00:01:09,990 Now will open up another terminal window down there. 21 00:01:09,990 --> 00:01:15,320 If you're not using I turn to go ahead and just create a third terminal window and change into the Nats 22 00:01:15,330 --> 00:01:18,100 test directory then. 23 00:01:18,100 --> 00:01:22,930 Inside there we will start up a second listener by running NPM run. 24 00:01:23,000 --> 00:01:27,240 Listen as soon as I run that I'm gonna end up getting an error message. 25 00:01:27,290 --> 00:01:29,930 It says client I.T. already registered. 26 00:01:29,930 --> 00:01:31,580 So what's going on here. 27 00:01:31,580 --> 00:01:35,120 Well as usual let's take a quick look at a diagram. 28 00:01:35,190 --> 00:01:42,140 All right so whenever we connect to our Nat streaming server the server maintains a list of different 29 00:01:42,140 --> 00:01:44,470 clients that it is connected to. 30 00:01:44,480 --> 00:01:49,860 So the first thing we had connect to our streaming server was the publisher when we created the publisher 31 00:01:50,910 --> 00:01:53,150 back inside of our publisher TSA file. 32 00:01:53,160 --> 00:01:58,530 The year publisher T.S. we called Nat Stock Connect and a second argument to it was something called 33 00:01:58,590 --> 00:01:59,870 a client I.D.. 34 00:02:00,120 --> 00:02:04,140 In this case we specified a client idea of simply ABC. 35 00:02:04,320 --> 00:02:09,300 So when we successfully connected the string server created a new client on the list of clients that 36 00:02:09,300 --> 00:02:16,470 was connected to it it said OK I've got some clients with a idea of ABC and I can connect to that client 37 00:02:16,530 --> 00:02:23,110 by reaching out to this publisher thing over here then a little bit later on inside of our listener 38 00:02:23,170 --> 00:02:27,120 s file we created a second client. 39 00:02:27,140 --> 00:02:31,120 It is right here and in this case we gave it a client idea of one two three. 40 00:02:32,000 --> 00:02:36,470 So once again we had the order service reach out connect and then that streaming server added to its 41 00:02:36,470 --> 00:02:37,500 list of clients. 42 00:02:37,580 --> 00:02:38,950 This brand new client. 43 00:02:39,160 --> 00:02:44,520 And it said OK you have an idea of one two 3. 44 00:02:44,620 --> 00:02:49,990 So now as we just tried to create a third or I should say really a second copy the listener or a third 45 00:02:49,990 --> 00:02:55,930 client overall we ran a second copy of the order service and so it tried to connect to the that's from 46 00:02:55,920 --> 00:03:03,130 the server with these same client ideas of 1 to 3 mastering server never wants to see a duplicate client 47 00:03:03,190 --> 00:03:03,770 I.D.. 48 00:03:04,070 --> 00:03:05,430 And that's exactly what we just did. 49 00:03:05,440 --> 00:03:10,810 We tried to connect with the same client I.D. and so that's why we saw the error message it said sorry 50 00:03:10,840 --> 00:03:18,630 but someone is already connected to the server with the client I.D. of 1 2 3 so if we want to run multiple 51 00:03:18,630 --> 00:03:21,720 copies of a listener publisher or whatever. 52 00:03:21,720 --> 00:03:25,980 And he said we want to run multiple clients we need to make sure that all these different clients have 53 00:03:26,010 --> 00:03:29,540 a different client i.e. in the world of Cuban eddies. 54 00:03:29,550 --> 00:03:31,690 There's only a very easy way for us to deal with that. 55 00:03:31,710 --> 00:03:36,360 But for right now in this test environment we're just going to say that we're going to randomly generate 56 00:03:36,480 --> 00:03:41,520 an idea so rather than having a hard coded one to three right here we will instead randomly generate 57 00:03:41,550 --> 00:03:47,650 a string and then provide it as the second argument to the Connect call so let's give that a shot right 58 00:03:47,650 --> 00:03:51,040 now to randomly generate a string at the very top to file. 59 00:03:51,040 --> 00:03:55,570 I'm going to import random bytes from 60 00:03:59,430 --> 00:04:04,020 you might recall using this function a bit earlier in the course and we are doing that password salting. 61 00:04:04,020 --> 00:04:09,810 So random bytes essentially just allows us to create a random string full of numbers and letters then 62 00:04:09,840 --> 00:04:11,510 I'll find the one two three right there. 63 00:04:11,510 --> 00:04:21,590 Going to delete it and replace it with random bytes or shoe string X like so if I now save this and 64 00:04:21,590 --> 00:04:26,690 put back over to my terminal it looks like both copies are now able to connect because they both have 65 00:04:26,710 --> 00:04:30,710 a random client I.D. OK that's definitely a good improvement. 66 00:04:30,730 --> 00:04:36,730 So we now know how to run multiple copies or multiple clients each of them with a randomly generated 67 00:04:36,790 --> 00:04:38,090 ideas. 68 00:04:38,100 --> 00:04:42,670 Now we're not quite done with this whole idea of having some multiple clients or multiple listeners. 69 00:04:42,720 --> 00:04:43,680 So a quick pause right here. 70 00:04:43,710 --> 00:04:47,610 We're going take a look at what happened when we really added in the second listener.