1
00:00:00,150 --> 00:00:05,800
All right, and up next, I want to talk about a Synchronoss patterns in Algiers.

2
00:00:06,360 --> 00:00:14,880
So if we remember when we were setting up the filesystem module, we covered how we have to flavour's.

3
00:00:15,150 --> 00:00:18,890
We have the Synchronoss one and the asynchronous one.

4
00:00:19,410 --> 00:00:26,280
And while asynchronous one is great, since we're not blocking the event loop, the problem is that

5
00:00:26,280 --> 00:00:33,360
if we're using this callback approach, well, it gets messy pretty quickly because we're not getting

6
00:00:33,660 --> 00:00:36,120
one callback inside of the other one.

7
00:00:36,510 --> 00:00:41,610
So now in the following videos, I would want to show you the alternative that we'll use throughout

8
00:00:41,610 --> 00:00:48,840
the course and why, in my opinion, it is much cleaner syntax and that's why, of course, we'll use

9
00:00:48,840 --> 00:00:48,950
it.

10
00:00:49,320 --> 00:00:52,770
And first, what I would want is just to kill everything here.

11
00:00:53,100 --> 00:00:59,610
And I'll start with the Nordman since I'm done showing you the event loop and we're just going to go

12
00:00:59,610 --> 00:01:01,560
out and start, right.

13
00:01:01,590 --> 00:01:03,000
That was the proper one.

14
00:01:03,270 --> 00:01:06,970
And now I'll delete everything that I currently have not.

15
00:01:06,990 --> 00:01:07,680
Yeah, sorry.

16
00:01:08,100 --> 00:01:09,240
So I'll leave this one.

17
00:01:09,240 --> 00:01:10,170
The async one.

18
00:01:10,440 --> 00:01:18,090
I would want to remove everything in the app and now I just want to quickly show you the code that would

19
00:01:18,090 --> 00:01:19,550
block that amental.

20
00:01:19,860 --> 00:01:25,770
Again, this is going to be the video where if you don't want to go to long after, you can just sit

21
00:01:25,770 --> 00:01:30,450
back and relax and effectively you'll be in good shape.

22
00:01:30,810 --> 00:01:35,730
And again, we'll set up the server, like I mentioned before, already twenty thousand times.

23
00:01:36,180 --> 00:01:38,190
Don't worry about specific commands.

24
00:01:38,790 --> 00:01:40,760
We have rest of the course.

25
00:01:40,880 --> 00:01:42,120
We'll be building server.

26
00:01:42,150 --> 00:01:44,270
So trust me, you'll get sick of it.

27
00:01:44,610 --> 00:01:48,450
So go with HP and require.

28
00:01:48,930 --> 00:01:55,170
And of course we're looking for HTP model and then remember the methods create server and listen.

29
00:01:55,530 --> 00:02:03,210
So we go with CONSED and we create the server that is HTP and create a server we pass in callback.

30
00:02:03,600 --> 00:02:07,180
Callback takes two objects, request object and response object.

31
00:02:07,500 --> 00:02:11,940
So again, essentially request object is what is coming in response.

32
00:02:12,120 --> 00:02:17,610
Object just represents what we're sending out, what essentially we are responding.

33
00:02:17,770 --> 00:02:20,010
And then I would want to listen on a board again.

34
00:02:20,010 --> 00:02:26,580
So I run server that listen and then I'll pass in the port of five thousand and then of course we have

35
00:02:26,580 --> 00:02:30,980
that callback once the server is ready and here we can just go.

36
00:02:31,230 --> 00:02:41,960
So log on or simply say server is listening on Port 5000, server is listening or just listening is

37
00:02:42,870 --> 00:02:43,710
listening.

38
00:02:44,880 --> 00:02:53,400
And worked online course, in my case, forty five thousand, let's set it up so we should have this

39
00:02:53,400 --> 00:03:00,150
listening on for 5000 men, of course, in my callback function, I would want to check for you URLs

40
00:03:00,210 --> 00:03:07,260
because I would want to show you how even if we try to get a different resource, we're still blocked

41
00:03:07,260 --> 00:03:13,950
by other user if he or she is requesting some kind of resource where we have the blocking code.

42
00:03:14,070 --> 00:03:18,760
And don't worry, if this sounds like gibberish, you'll see what I mean in a second.

43
00:03:19,050 --> 00:03:25,260
So remember, on the request object we could check for, you are all so effectively you could check

44
00:03:25,440 --> 00:03:28,720
what is the resource that the user is requesting.

45
00:03:28,750 --> 00:03:34,940
So, for example, home page would be forward slash and the about page would be forgotten about.

46
00:03:35,060 --> 00:03:39,090
And that is sitting in the property by the name of request.

47
00:03:39,300 --> 00:03:43,900
And you are also requesting the object and then you URL is the property.

48
00:03:44,160 --> 00:03:46,470
So again, we'll do a quick and dirty way.

49
00:03:46,470 --> 00:03:48,310
We'll just say request.

50
00:03:48,310 --> 00:03:51,330
The URL is equal to a force.

51
00:03:51,720 --> 00:03:56,040
That means it's a home page and then we just need to go with a red dot.

52
00:03:56,040 --> 00:03:59,850
And um, I will say home page again later.

53
00:03:59,850 --> 00:04:02,370
We'll have way more sophisticated approach.

54
00:04:02,370 --> 00:04:04,170
But for now this will do.

55
00:04:04,810 --> 00:04:07,170
And we're checking for the about page.

56
00:04:07,890 --> 00:04:09,370
Click some.

57
00:04:09,600 --> 00:04:11,460
So this is going to be an about page.

58
00:04:11,790 --> 00:04:19,380
And then if there is a page that doesn't match any of the resources, then of course I'll just send

59
00:04:19,380 --> 00:04:21,540
back, I don't know, error page.

60
00:04:21,959 --> 00:04:23,330
I'm not going to set up the whole link.

61
00:04:23,610 --> 00:04:24,410
Nothing like that.

62
00:04:25,370 --> 00:04:33,650
All right, we say it, and this should send back about page some and of course, in homepage, I can

63
00:04:33,650 --> 00:04:35,290
just respond with the homepage.

64
00:04:35,690 --> 00:04:36,780
So life is beautiful.

65
00:04:37,040 --> 00:04:39,500
Users can come here and sign out.

66
00:04:39,800 --> 00:04:42,950
Multiple users are presenting with these tabs.

67
00:04:42,950 --> 00:04:46,850
So imagine these are my users and life is beautiful.

68
00:04:47,180 --> 00:04:51,500
We are requesting resources and we're just going on about our day.

69
00:04:51,890 --> 00:04:59,660
Now, the problem is going to be if I go to about and if I set up some kind of blocking code now, what

70
00:04:59,660 --> 00:05:00,990
would be a blocking code?

71
00:05:01,010 --> 00:05:03,850
Well, that could be a nested for loop.

72
00:05:04,310 --> 00:05:10,280
So I'm just going to go with blocking code and then I'll add some exclamation points and let's simply

73
00:05:10,280 --> 00:05:23,210
set up a up, let's say I is equal to zero and then let's say I is less than a thousand and I plus plus.

74
00:05:23,240 --> 00:05:27,280
So every iteration will increase by one hour.

75
00:05:27,740 --> 00:05:30,190
I'll set it up as a nested portal.

76
00:05:30,500 --> 00:05:34,940
So copy and paste and I'll just change these values to J.

77
00:05:35,830 --> 00:05:38,260
So Joe here and Joe here.

78
00:05:39,150 --> 00:05:46,840
And this just comes along and we'll say, Jini, again, doesn't really matter what we type there and

79
00:05:46,970 --> 00:05:53,330
just say I am J now, no question, what do you think is going to happen?

80
00:05:54,330 --> 00:05:57,030
So if the user navigates to about.

81
00:05:57,880 --> 00:06:01,210
I mean, we can kind of see that this will take some time.

82
00:06:01,750 --> 00:06:08,410
Right now, you would expect, though, that only the user who navigates here gets blocked.

83
00:06:09,130 --> 00:06:16,360
And you would be wrong, because we'll also block the other suckers who are just trying to get to the

84
00:06:16,360 --> 00:06:16,810
homepage.

85
00:06:17,260 --> 00:06:20,130
So let me navigate here and check this out.

86
00:06:20,140 --> 00:06:23,220
So I run this one to notice how we're loading now.

87
00:06:23,280 --> 00:06:25,600
The same thing happens here now and here.

88
00:06:26,020 --> 00:06:31,960
OK, and only when we are done here, then these two can get the resource again.

89
00:06:32,590 --> 00:06:38,500
Now, of course, if I go to my project, I can see that in the console.

90
00:06:38,770 --> 00:06:40,240
I have all these values right.

91
00:06:40,510 --> 00:06:42,580
Because I have this blocking code.

92
00:06:42,580 --> 00:06:46,470
I have the synchronous code that just takes a long time.

93
00:06:46,810 --> 00:06:54,320
And this is just a representation of why we prefer this asynchronous now.

94
00:06:54,340 --> 00:06:58,720
Yes, the moment this is messy and the worry in the next video will fix it.

95
00:06:59,110 --> 00:07:05,410
But this hopefully gives you an idea of why you should always strive setting up your code asynchronously,

96
00:07:06,010 --> 00:07:14,140
because what we've learned in the previous videos, if we do that, then those tasks are uploaded online.

97
00:07:14,170 --> 00:07:20,110
Only when the data is back, only when it's ready, then we invoke it.

98
00:07:20,410 --> 00:07:23,320
And that way we're not blocking the other users.

99
00:07:23,570 --> 00:07:24,520
Hopefully it is clear.

100
00:07:24,520 --> 00:07:30,370
And now, of course, let's take a look at some other patterns we can use so we still get the benefits.

101
00:07:30,730 --> 00:07:40,360
So we're not blocking that event loop, but we also have a cleaner code where we don't have this nested

102
00:07:40,630 --> 00:07:41,900
callback mess.

