Hi everyone.
In this video I will talk about the one more API of Socket.io will be the namespace.
Okay, you can think a namespace is just some channel.
Okay, it will isolation the event.
And listen on the event okay.
For example, by default when we create the.
io with the new server, it will create some pools or it will create some channel will be the default
channel okay.
And whenever we listening on the event or emit the event okay, we only interact with this particular
channel over here.
The default channel okay.
And maybe in the future.
We want to create another channel for admin.
Okay, only the admin channel can listening on this event.
Over here.
Okay.
We will create another namespace.
Okay.
That's the concept of namespace.
You can search inside the server API of the Socket.io and scroll down and click inside the namespace over
here.
Okay.
You can read more about that.
Let me show you in practice.
By default, when we create the IO right here.
We will create a default namespace.
Okay.
If you want to create another namespace for example like admin namespace I will create admin io
dot of will be /admin
Okay, in order to separate the channel, we will use the slash and the some.
It's like the URL.
Now you can see right here it will identify by the path name okay.
And let me show you what I mean inside here.
Are you listening into the connection.
Like socket.
Okay.
And I will console.log like client connect.
To admin namespace.
Okay.
And I will open the terminal right here.
If I refresh multiple times, you can see it only trigger.
Inside the default channel okay.
Now how we can establish the client with the server using the admin channel.
Well, in order to do that we will scroll back over here.
When we create Socket.io we actually create a slash.
Okay.
This will be the default channel and it will interact with the io over here.
Let me show you what I mean.
If I refresh you can see we will console.log the connect over here, right?
It means by default it will automatically pass.
The default channel will be the slash over here.
Well, in order to create a different channel, we can remove that.
I will create the admin socket with the io right here I will create admin channel.
And right here we will listen in.
Right here.
Let me listening.
Actually.
Right here, let me try to refresh the application.
Now if I refresh the application, you can see we already connect to the admin namespace with that id
okay.
It mean right here, we already established the connection okay.
And in order to emit the event into a particular connection, we can use right here.
Let me look for example right here.
Let me see right here I will use admin
I will emit the event to the.
I will broadcast the event right.
I will emit the event into all the client has connected to admin channel we will emit.
greating, for example.
I'm sorry.
Well, channel and inside a script right here.
We can listen in on that, but right now we will listen in into the admin socket.
Okay.
So here you can see we already got message okay.
And that's how we can create a channel for the socket io okay.
And that's for the video I will see you in the next one.