At this point our little test application has exactly one publisher and one listener inside of it.
Now let's imagine that we eventually deploy this test application into our Cuban cities cluster and
maybe we use it for a real application at some point in time we might realize that the order service
is getting a lot of traffic and so we might decide to scale up the order service in some way.
We can either scale the order service vertically by giving the order service some more powerful CPE
you more ram and so on.
Or alternatively we could try to scale it horizontally by just creating a second instance of the order
service so we might have two copies of the exact same service and in all regards they're identical in
nature.
Now of course we would want to make sure that both these services would be able to connect to our Nat
streaming server.
Let's try opening up our terminal once again inside the terminal.
Let's create a second window.
I guess the third window in this case and tried running another copy of the order service and just see
what happens.
All right.
So back inside my terminal.
If you are running I turned 2.
You can select a window that's running the listener and then do a command shift D.
Now will open up another terminal window down there.
If you're not using I turn to go ahead and just create a third terminal window and change into the Nats
test directory then.
Inside there we will start up a second listener by running NPM run.
Listen as soon as I run that I'm gonna end up getting an error message.
It says client I.T. already registered.
So what's going on here.
Well as usual let's take a quick look at a diagram.
All right so whenever we connect to our Nat streaming server the server maintains a list of different
clients that it is connected to.
So the first thing we had connect to our streaming server was the publisher when we created the publisher
back inside of our publisher TSA file.
The year publisher T.S. we called Nat Stock Connect and a second argument to it was something called
a client I.D..
In this case we specified a client idea of simply ABC.
So when we successfully connected the string server created a new client on the list of clients that
was connected to it it said OK I've got some clients with a idea of ABC and I can connect to that client
by reaching out to this publisher thing over here then a little bit later on inside of our listener
s file we created a second client.
It is right here and in this case we gave it a client idea of one two three.
So once again we had the order service reach out connect and then that streaming server added to its
list of clients.
This brand new client.
And it said OK you have an idea of one two 3.
So now as we just tried to create a third or I should say really a second copy the listener or a third
client overall we ran a second copy of the order service and so it tried to connect to the that's from
the server with these same client ideas of 1 to 3 mastering server never wants to see a duplicate client
I.D..
And that's exactly what we just did.
We tried to connect with the same client I.D. and so that's why we saw the error message it said sorry
but someone is already connected to the server with the client I.D. of 1 2 3 so if we want to run multiple
copies of a listener publisher or whatever.
And he said we want to run multiple clients we need to make sure that all these different clients have
a different client i.e. in the world of Cuban eddies.
There's only a very easy way for us to deal with that.
But for right now in this test environment we're just going to say that we're going to randomly generate
an idea so rather than having a hard coded one to three right here we will instead randomly generate
a string and then provide it as the second argument to the Connect call so let's give that a shot right
now to randomly generate a string at the very top to file.
I'm going to import random bytes from
you might recall using this function a bit earlier in the course and we are doing that password salting.
So random bytes essentially just allows us to create a random string full of numbers and letters then
I'll find the one two three right there.
Going to delete it and replace it with random bytes or shoe string X like so if I now save this and
put back over to my terminal it looks like both copies are now able to connect because they both have
a random client I.D. OK that's definitely a good improvement.
So we now know how to run multiple copies or multiple clients each of them with a randomly generated
ideas.
Now we're not quite done with this whole idea of having some multiple clients or multiple listeners.
So a quick pause right here.
We're going take a look at what happened when we really added in the second listener.