WEBVTT

1
00:00.760 --> 00:02.240
Hi everyone.

2
00:02.240 --> 00:12.440
In this video I will talk about the one more API of Socket.io will be the namespace.

3
00:13.280 --> 00:18.000
Okay, you can think a namespace is just some channel.

4
00:18.240 --> 00:21.920
Okay, it will isolation the event.

5
00:24.120 --> 00:28.600
And listen on the event okay.

6
00:29.400 --> 00:34.160
For example, by default when we create the.

7
00:36.280 --> 00:45.200
io with the new server, it will create some pools or it will create some channel will be the default

8
00:45.200 --> 00:46.800
channel okay.

9
00:46.840 --> 00:58.840
And whenever we listening on the event or emit the event okay, we only interact with this particular

10
00:58.840 --> 01:00.080
channel over here.

11
01:00.480 --> 01:02.880
The default channel okay.

12
01:03.920 --> 01:06.120
And maybe in the future.

13
01:06.440 --> 01:10.360
We want to create another channel for admin.

14
01:10.400 --> 01:16.760
Okay, only the admin channel can listening on this event.

15
01:18.960 --> 01:19.720
Over here.

16
01:20.400 --> 01:20.920
Okay.

17
01:21.120 --> 01:23.480
We will create another namespace.

18
01:23.640 --> 01:23.920
Okay.

19
01:23.960 --> 01:25.440
That's the concept of namespace.

20
01:25.600 --> 01:32.880
You can search inside the server API of the Socket.io and scroll down and click inside the namespace over

21
01:32.880 --> 01:33.240
here.

22
01:34.080 --> 01:34.360
Okay.

23
01:34.400 --> 01:36.480
You can read more about that.

24
01:39.760 --> 01:41.240
Let me show you in practice.

25
01:42.600 --> 01:46.560
By default, when we create the IO right here.

26
01:48.720 --> 01:53.960
We will create a default namespace.

27
01:54.560 --> 01:55.080
Okay.

28
01:56.680 --> 02:06.520
If you want to create another namespace for example like admin namespace I will create admin io

29
02:08.600 --> 02:11.950
dot of will be /admin

30
02:12.950 --> 02:19.710
Okay, in order to separate the channel, we will use the slash and the some.

31
02:20.030 --> 02:21.630
It's like the URL.

32
02:22.110 --> 02:27.990
Now you can see right here it will identify by the path name okay.

33
02:29.710 --> 02:33.390
And let me show you what I mean inside here.

34
02:33.990 --> 02:37.830
Are you listening into the connection.

35
02:39.350 --> 02:39.750
Like socket.

36
02:39.750 --> 02:40.230
Okay.

37
02:42.110 --> 02:47.670
And I will console.log like client connect.

38
02:49.430 --> 02:52.270
To admin namespace.

39
02:56.230 --> 02:56.710
Okay.

40
02:56.710 --> 03:00.310
And I will open the terminal right here.

41
03:01.670 --> 03:06.430
If I refresh multiple times, you can see it only trigger.

42
03:06.470 --> 03:09.710
Inside the default channel okay.

43
03:10.350 --> 03:17.990
Now how we can establish the client with the server using the admin channel.

44
03:18.510 --> 03:21.950
Well, in order to do that we will scroll back over here.

45
03:22.710 --> 03:27.510
When we create Socket.io we actually create a slash.

46
03:27.670 --> 03:27.990
Okay.

47
03:28.030 --> 03:33.990
This will be the default channel and it will interact with the io over here.

48
03:34.750 --> 03:36.030
Let me show you what I mean.

49
03:36.510 --> 03:42.190
If I refresh you can see we will console.log the connect over here, right?

50
03:43.110 --> 03:48.350
It means by default it will automatically pass.

51
03:48.350 --> 03:52.870
The default channel will be the slash over here.

52
03:53.750 --> 03:57.590
Well, in order to create a different channel, we can remove that.

53
03:58.870 --> 04:07.870
I will create the admin socket with the io right here I will create admin channel.

54
04:08.830 --> 04:10.990
And right here we will listen in.

55
04:16.870 --> 04:17.470
Right here.

56
04:17.670 --> 04:19.590
Let me listening.

57
04:25.710 --> 04:26.430
Actually.

58
04:28.510 --> 04:33.750
Right here, let me try to refresh the application.

59
04:33.790 --> 04:40.150
Now if I refresh the application, you can see we already connect to the admin namespace with that id

60
04:40.190 --> 04:40.430
okay.

61
04:40.470 --> 04:45.790
It mean right here, we already established the connection okay.

62
04:47.030 --> 04:56.550
And in order to emit the event into a particular connection, we can use right here.

63
04:57.110 --> 05:00.110
Let me look for example right here.

64
05:00.110 --> 05:05.350
Let me see right here I will use admin

65
05:07.870 --> 05:10.110
I will emit the event to the.

66
05:12.110 --> 05:14.150
I will broadcast the event right.

67
05:15.750 --> 05:24.310
I will emit the event into all the client has connected to admin channel we will emit.

68
05:27.420 --> 05:28.620
greating, for example.

69
05:41.940 --> 05:42.500
I'm sorry.

70
05:45.940 --> 05:51.260
Well, channel and inside a script right here.

71
05:51.260 --> 05:56.860
We can listen in on that, but right now we will listen in into the admin socket.

72
05:56.900 --> 05:57.300
Okay.

73
06:14.140 --> 06:17.380
So here you can see we already got message okay.

74
06:17.420 --> 06:26.260
And that's how we can create a channel for the socket io okay.

75
06:28.260 --> 06:31.660
And that's for the video I will see you in the next one.