WEBVTT

1
00:01.240 --> 00:02.880
Hello everyone!

2
00:03.320 --> 00:16.520
In this video I will show you how we can send and receive the data using Socket.io.

3
00:18.120 --> 00:22.880
But before that, let me show you inside the callback.

4
00:22.880 --> 00:30.880
Right here we will have a one parameter called a socket.

5
00:31.920 --> 00:36.600
Inside here we can use socket.id

6
00:37.280 --> 00:46.080
We will print the unique identifier of the socket connection right here okay.

7
00:46.720 --> 00:54.800
Every time we refresh the application we will get a different ID okay.

8
00:57.320 --> 01:05.760
And of course when we open the new connection it will have a connection over here with the new id.

9
01:05.800 --> 01:10.320
Right now let's see you how we can send the data.

10
01:12.360 --> 01:12.920
Over.

11
01:14.160 --> 01:17.040
For example, inside the server.

12
01:17.080 --> 01:21.880
I want to send the data into the socket.

13
01:22.240 --> 01:22.800
Okay.

14
01:22.840 --> 01:24.920
Into particular socket.

15
01:30.440 --> 01:39.480
Well, in order to send event inside the socket, we will use the emit okay.

16
01:39.760 --> 01:46.800
With emit we will register the event name for example like welcome okay.

17
01:46.800 --> 01:48.720
Like that, welcome over here.

18
01:50.280 --> 02:08.320
And what we need to print right now is I will send the data like hi with the particular id okay.

19
02:08.360 --> 02:14.520
I will send this message right here for this.

20
02:17.400 --> 02:18.120
Client.

21
02:18.840 --> 02:19.360
Okay.

22
02:20.640 --> 02:22.600
And how we can receive that?

23
02:25.120 --> 02:27.400
Inside the socket right here.

24
02:28.200 --> 02:34.680
We will listen in into the event, and we pass the event name.

25
02:35.080 --> 02:36.360
What's the event name?

26
02:36.400 --> 02:40.600
It must be exactly the same right here.

27
02:42.080 --> 02:42.600
Okay.

28
02:44.520 --> 02:48.480
And we need to pass the callback like message.

29
02:49.120 --> 02:51.520
And right here I will console log message.

30
02:52.120 --> 02:52.880
Now let's see.

31
02:56.400 --> 03:00.040
I will expect and refresh.

32
03:00.360 --> 03:05.840
Now you can see we already got the hi with this ID.

33
03:06.360 --> 03:07.880
Now you can see the ID right here.

34
03:08.320 --> 03:12.640
And the id right here must be exactly the same, right?

35
03:13.880 --> 03:15.000
Exactly the same.

36
03:17.440 --> 03:21.520
And if I connect.

37
03:22.610 --> 03:23.450
right here.

38
03:24.690 --> 03:29.530
You will see we also got the new client.

39
03:30.410 --> 03:32.850
Okay, that's how we can register.

40
03:33.090 --> 03:37.690
And that's what we call the event based API.

41
03:40.810 --> 03:41.210
Okay.

42
03:41.210 --> 03:45.570
You can see it's much more cleaner than the previous section.

43
03:45.850 --> 03:49.650
We we don't need to check every time.

44
03:49.650 --> 03:50.090
Right.

45
03:50.330 --> 03:53.970
Instead of we just need to register the event name.

46
03:54.290 --> 03:57.690
And right here we need to listen in on that.

47
03:59.810 --> 04:04.290
And of course from the client we can also send the data.

48
04:09.650 --> 04:11.450
Okay for example like

49
04:15.610 --> 04:16.170
Message.

50
04:19.050 --> 04:23.930
My message from client.

51
04:24.850 --> 04:30.010
And also inside the server we can listen on that

52
04:38.730 --> 04:41.250
And we need to pass the client.

53
04:44.410 --> 04:44.970
Message.

54
04:47.370 --> 04:47.930
Okay.

55
04:59.690 --> 05:06.410
So now I will refresh and you can see we got the message from the client.

56
05:08.610 --> 05:09.090
Okay.

57
05:09.130 --> 05:10.210
Very very cool.

58
05:10.610 --> 05:11.010
Okay.

59
05:11.050 --> 05:18.050
We will listen the event on the particular socket.

60
05:18.490 --> 05:20.930
That's what it means about the socket right here.

61
05:20.970 --> 05:23.850
It's very similar with the fs.

62
05:24.930 --> 05:25.330
Sorry.

63
05:25.370 --> 05:29.850
The "ws" from the previous session.

64
05:31.530 --> 05:34.690
So that is that's about the video.

65
05:34.890 --> 05:36.130
I will see you in the next.