forked from marijnh/Eloquent-JavaScript
/
21_skillsharing.txt
1281 lines (1040 loc) · 44.9 KB
/
21_skillsharing.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
:chap_num: 21
:prev_link: 20_node
= Project: Skill-Sharing Website =
(((project chapter)))A _skill-sharing_ meeting is an event where
people with a shared interest come together and give small, informal
presentations about things they know. At a gardening skill-sharing
meeting, someone might explain how to cultivate celery. Or in a
programming-oriented skill-sharing group, you could drop by and tell
everybody about Node.js.
Such meetups, also often called _users’ groups_ when they are about
computers, are a great way to broaden your horizon, learn about new
developments, or simply meet people with similar interests. Many large
cities have a JavaScript meetup. They are typically free to attend,
and I've found the ones I've visited to be friendly and welcoming.
In this final project chapter, our project is to set up a website for
managing talks given at a skill-sharing meeting. Imagine a small group
of people, meeting up regularly in one of the members’ office to talk
about unicycling. Problem is: when the previous organizer of the
meetings moved to another town, nobody stepped forward to take over
this task. We want a system that will let the participants propose and
discuss talks among themselves, without a central organizer.
image::img/unicycle.svg[alt="The unicycling meetup"]
(!html Just like in the link:20_node.html#node[previous chapter], the
code in this chapter is written for Node.js, and running it directly
in the HTML page that you are looking at is unlikely to work. !)The
full code for the project can be downloaded from
http://eloquentjavascript.net/code/skillsharing.zip[_eloquentjavascript.net/code/skillsharing.zip_].
== Design ==
There is a _server_ part to this project, written for Node.js, and a
_client_ part, written for the browser. The server stores the system's
data and provides it to the client. It also serves the HTML and
JavaScript files that implement the client-side system.
The server keeps a list of talks proposed for the next meeting, and
the client shows this list. Each talk has a presenter name, a title, a
summary, and a list of comments associated with it. The client allows
users to propose new talks (adding them to the list), delete talks,
and to comment on existing talks. Whenever the user makes such a
change, the client makes an HTTP request to tell the server about it.
image::img/skillsharing.png[alt="Screenshot of the skill-sharing website",width="10cm"]
The application will be set up to show a _live_ view of the current
proposed talks and their comments. Whenever someone, somewhere,
submits a new talk or adds a comment, all people who have the page
open in their browsers should immediately see the change. This poses a
bit of a challenge, since there is no way for a web server to open up
a connection to a client, nor is there a good way to know which
clients currently are looking at a given website.
A common solution to this problem is called _long polling_, which
happens to be one of the motivations for Node's design.
== Long polling ==
To be able to immediately notify a client that something changed, we
need a connection to that client. Since web browsers do not
traditionally accept connections, and clients are usually behind
devices that would block such connections anyway, having the server
initiate this connection is not practical.
We can arrange for the client to open the connection, and keep it
around, so that the server can use it to send information when it
needs to.
But an HTTP request allows only a simple flow of information, where
the client sends a request, the server comes back with a single
response, and that is it. There is a technology called _web sockets_,
supported by modern browsers, which makes it possible to open
connections for arbitrary data exchange. But using them properly is
somewhat tricky.
In this chapter, we will use a relatively simple technique, long
polling, where clients keeps asking the server for new information,
using regular HTTP requests, and the server simply stalls its
answer when it has nothing new to report.
As long as the client makes sure it constantly has a polling request
open, it will receive information from the server immediately. For
example, if Alice has our skill sharing application open in her
browser, that browser will have made a request for updates, and be
waiting for a response to that request. When Bob, from his own
browser, submits a talk on Extreme Downhill Unicycling, the server
will notice that Alice is waiting for updates, and send information
about the new talk as a response to her pending request. Alice's
browser will receive the data, and update the screen to show the talk.
To prevent connections from timing out (being aborted due to a lack of
activity), long polling techniques usually set a maximum time for each
request, after which the server will respond anyway, even though it
has nothing to report, and the client will start a new request.
Periodically restarting the request also makes the technique more
robust, allowing clients to recover from temporary connection failures
or server problems.
A busy server that is using long polling may have thousands of waiting
request, and thus TCP connections, open. Node, which makes it easy to
manage many connections without creating a separate thread of control
for each one, is a good fit for such a system.
== HTTP interface ==
Before we start fleshing out either the server or the client, let's
think about the point where they touch: the HTTP interface over which
they communicate.
We will base our interface on JSON, and, like in the file server from
link:20_node.html#file_server[Chapter 20], try to make good use of
HTTP verbs. The interface is centered around the path `/talks`. Paths
that do not start with `/talks` will be used for serving static
files—the HTML and JavaScript code that implement the client-side
system.
A `GET` request to `/talks` returns a JSON document like this:
[source,application/json]
----
{"serverTime": 1405438911833,
"talks": [{"title": "Unituning",
"presenter": "Carlos",
"summary": "Modifying your cycle for extra style",
"comment": []}]}
----
The `serverTime` field will be used to make reliable long polling
possible. I will come back to it
link:21_skillsharing.html#poll_time[later].
Creating a new talk is done by making a `PUT` request to a URL like
`/talks/Unituning`, where the part after the second slash is the title
of the talk. The `PUT` request's body should contain a JSON object
that has `presenter` and `summary` properties.
Since talk titles may contain spaces and other characters that may not
appear normally in a URL, they must be encoded when building up such a
URL, using the `encodeURIComponent` function.
[source,javascript]
----
console.log("/talks/" + encodeURIComponent("How to Idle"));
// → /talks/How%20to%20Idle
----
A request to create a talk about idling might look something like
this:
[source,http]
----
PUT /talks/How%20to%20Idle HTTP/1.1
Content-Type: application/json
Content-Length: 92
{"presenter": "Dana",
"summary": "Standing still on a unicycle"}
----
Such URLs also support `GET` requests to retrieve the JSON
representation of a talk, and `DELETE` requests to delete a talk.
Adding a comment to a talk is done with a `POST` request to a URL like
`/talks/Unituning/comments`, with a JSON object that has `author` and
`message` properties as the body of the request.
[source,http]
----
POST /talks/Unituning/comments HTTP/1.1
Content-Type: application/json
Content-Length: 72
{"author": "Alice",
"message": "Will you talk about raising a cycle?"}
----
To support long polling, `GET` requests to `/talks` may include a
query parameter `changesSince`, which is used to indicate that the
client is interested in updates that happened since a given point in
time. When there are such changes, they are immediately returned. When
not, the response is delayed until something happens, or a given time
period (we will use 90 seconds) has elapsed.
[[poll_time]]
The time must be indicated as the number of milliseconds elapsed since
the start of 1970, the same type of number that is returned by
`Date.now()`. In order to ensure that it receives all updates, and
doesn't receive the same update more than once, the client must pass
the time at which it last received information from the server. The
server's clock might not be exactly in sync with the client's clock,
and even if it were, it will be impossible for the client to know
the precise time at which the server sent a response, due to the fact
that transferring data over the network takes time.
This is the reason for the existence of the `serverTime` property in
responses sent to `GET` requests to `/talks`. It tells the client the
precise time, from the server's perspective, at which the data it
receives was created. It can then simply store this time, and pass it
along in its next polling request, to make sure that it receives
exactly the updates that it has not seen before.
----
GET /talks?changesSince=1405438911833 HTTP/1.1
(time passes)
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 95
{"serverTime": 1405438911833,
"talks": [{"title": "Unituning",
"deleted": true}]}
----
When a talk has been changed, newly created, or has a comment added,
the full representation of the talk is included in the response to
such a request. When a talk is deleted, only its title and the
property `deleted` are included. The client can then add talks with
titles it has not seen before to its display, update talks that it was
already showing, and remove those that were deleted.
The protocol described here does not do any access control. Everybody
can comment, modify talks, and even delete them. Since the internet is
filled with hooligans, putting such a system online without further
protection is likely to end in disaster.
One solution would be to put it behind a _reverse proxy_, which is a
system that accepts HTTP connections from outside the system, and
forwards them to HTTP servers that are running locally. Such a proxy
can be configured to require a user name and password, and you could
make sure only the participants in the skill sharing group have this
password.
This is rather crude, and a better solution would be to add
functionality for identifying users directly in our server, possibly
using a system like Mozilla Persona
(https://www.mozilla.org/persona[_www.mozilla.org/persona_]), which
allows people to use a single password for multiple sites, and makes
it unnecessary for each separate system to store passwords (which is
always risky). But all that is outside of the scope of this chapter.
== The server ==
Let's start by writing the server-side part of the program. The
following code runs on Node.js.
=== Routing ===
Our server will use `http.createServer` to start an HTTP server. In
the function that handles a new request, we must distinguish between
the various kinds of requests (as determined by the method and the
path) that we support. This can be done with a long chain of `if`
statements, but there is a nicer way.
A _router_ is a component that helps dispatch a request to the
function that can handle it. You can tell it, for example, that
requests with the `PUT` method which have a path that matches the
regular expression `/^\/talks\/([^\/]+)$/` (`/talks/` followed by a
talk title) can be handled by a given function. In addition it can
help extract the meaningful parts of the path, in this case the talk
title, wrapped in parentheses in the regular expression, and pass
those to the handler function.
There are a number of good router packages on NPM, but here we will
write one ourselves, to illustrate the principle.
This is `router.js`, which we will later `require` from our server
module:
// include_code >code/skillsharing/router.js
[source,javascript]
----
var Router = module.exports = function() {
this.routes = [];
};
Router.prototype.add = function(method, url, handler) {
this.routes.push({method: method,
url: url,
handler: handler});
};
Router.prototype.resolve = function(request, response) {
var path = require("url").parse(request.url).pathname;
return this.routes.some(function(route) {
var match = route.url.exec(path);
if (!match || route.method != request.method)
return false;
var urlParts = match.slice(1).map(decodeURIComponent);
route.handler.apply(null, [request, response]
.concat(urlParts));
return true;
});
};
----
The module exports the `Router` constructor. A router object allows
new handlers to be registered with the `add` method, and can resolve
requests with its `resolve` method.
The latter will return a Boolean that indicates whether a handler was
found. Can you see how the `some` method on the array of routes will
try the routes one at a time (in the order in which they were
defined), and stop, returning `true`, when a matching one is found?
The handler functions are called with the `request` and `response`
objects. When the regular expression that matches the URL contains any
groups, these are URL-decoded (since the raw URL will contain
`%20`-style codes), and passed to the handler as extra arguments.
=== Serving files ===
When a request matches none of the request types defined in our
router, the server must interpret it as being a request for a file in
the `public` directory. It would be possible to use the file server
defined in link:20_node.html#file_server[Chapter 20] to serve such
files, but since we neither need nor want to support `PUT` and
`DELETE` requests on files, and we would like to have advanced
features such as support for caching, let's use a solid, well-tested
static file server from NPM instead.
I opted for `ecstatic`, which isn't the only such server on NPM, but
which works well and fits our purposes. The `ecstatic` module exports
a function that can be called with a configuration object to produce a
request handler function. We use the `root` option to tell the server
where it should look for files. The handler function accepts `request`
and `response` parameters, and can be passed directly to
`createServer` to create a server that _only_ serves files. We want to
first check for requests that we handle specially though, so we wrap
it in another function.
// include_code >code/skillsharing/skillsharing_server.js
[source,javascript]
----
var http = require("http");
var Router = require("./router");
var ecstatic = require("ecstatic");
var fileServer = ecstatic({root: "./public"});
var router = new Router();
http.createServer(function(request, response) {
if (!router.resolve(request, response))
fileServer(request, response);
}).listen(8000);
----
The following helper functions are used throughout the server code to
be able to send off responses with a single function call.
// include_code >code/skillsharing/skillsharing_server.js
[source,javascript]
----
function respond(response, status, data, type) {
response.writeHead(status, {
"Content-Type": type || "text/plain"
});
response.end(data);
}
function respondJSON(response, status, data) {
respond(response, status, JSON.stringify(data),
"application/json");
}
----
=== Talks as resources ===
The server keeps the talks that have been proposed in an object called
`talks`, whose property names are the talk titles. These will be
exposed as HTTP resources under `/talks/[title]`, so we need to add
handlers to our router that implement the various methods that clients
can use to work with them.
The handler for requests that `GET` a single talk is very simple.
// include_code >code/skillsharing/skillsharing_server.js
[source,javascript]
----
var talks = Object.create(null);
router.add("GET", /^\/talks\/([^\/]+)$/,
function(request, response, title) {
if (title in talks)
respondJSON(response, 200, talks[title]);
else
respond(response, 404, "No talk '" + title + "' found");
});
----
Deleting a talk is also not hard.
// include_code >code/skillsharing/skillsharing_server.js
[source,javascript]
----
router.add("DELETE", /^\/talks\/([^\/]+)$/,
function(request, response, title) {
if (title in talks) {
delete talks[title];
registerChange(title);
}
respond(response, 204, null);
});
----
The `registerChange` function, which we will define
link:21_skillsharing.html#registerChange[later], notifies waiting long
polling requests about the change.
To be able to easily get the content of JSON-encoded request bodies,
we define a function `readStreamAsJSON`, which reads all content from
a stream, parses it as JSON, and then calls a callback function.
// include_code >code/skillsharing/skillsharing_server.js
[source,javascript]
----
function readStreamAsJSON(stream, callback) {
var data = "";
stream.on("data", function(chunk) {
data += chunk;
});
stream.on("end", function() {
var result, error;
try { result = JSON.parse(data); }
catch (e) { error = e; }
callback(error, result);
});
stream.on("error", function(error) {
callback(error);
});
}
----
One of the handlers that needs to read JSON responses is the `PUT`
handler used to create new talks. It has to checks whether the data it
was given has `presenter` and `summary` properties which are strings.
Any data coming from outside the system might be nonsense, and we
don't want to corrupt our internal data model, or even crash, when bad
requests come in.
If the data looks valid, the handler stores an object that represents
the new talk in the `talks` object, possibly overwriting an existing
talk with this title, and again calls `registerChange`.
// include_code >code/skillsharing/skillsharing_server.js
[source,javascript]
----
router.add("PUT", /^\/talks\/([^\/]+)$/,
function(request, response, title) {
readStreamAsJSON(request, function(error, talk) {
if (error) {
respond(response, 400, error.toString());
} else if (!talk ||
typeof talk.presenter != "string" ||
typeof talk.summary != "string") {
respond(response, 400, "Bad talk data");
} else {
talks[title] = {title: title,
presenter: talk.presenter,
summary: talk.summary,
comments: []};
registerChange(title);
respond(response, 204, null);
}
});
});
----
Adding a comment to a talk is done in a very similar way. We use
`readStreamAsJSON` to get the content of the request, validate the
resulting data, and store it as a comment when it looks valid.
// include_code >code/skillsharing/skillsharing_server.js
[source,javascript]
----
router.add("POST", /^\/talks\/([^\/]+)\/comments$/,
function(request, response, title) {
readStreamAsJSON(request, function(error, comment) {
if (error) {
respond(response, 400, error.toString());
} else if (!comment ||
typeof comment.author != "string" ||
typeof comment.message != "string") {
respond(response, 400, "Bad comment data");
} else if (title in talks) {
talks[title].comments.push(comment);
registerChange(title);
respond(response, 204, null);
} else {
respond(response, 404, "No talk '" + title + "' found");
}
});
});
----
Trying to add a comment to a non-existing talk should return a 404
error, of course.
=== Long polling support ===
The most interesting aspect of the server is the part that handles
long polling. When a `GET` request comes in for `/talks`, it can
either be a simple request for all talks, or it is a request for
updates, with a `changesSince` parameter.
There'll be various situations in which we have to send a list of
talks to the client, so we first define a small helper function that
attaches the `serverTime` field to such responses.
// include_code >code/skillsharing/skillsharing_server.js
[source,javascript]
----
function sendTalks(talks, response) {
respondJSON(response, 200, {
serverTime: Date.now(),
talks: talks
});
}
----
The handler itself needs to look at the query parameters in the
request's URL, to see whether a `changesSince` parameter is given. The
`"url"` module's `parse` function will, if you give it a second
argument of `true`, also parse the query part of a URL. The object it
returns will have a `query` property, holding another object that maps
parameter names to values.
// include_code >code/skillsharing/skillsharing_server.js
[source,javascript]
----
router.add("GET", /^\/talks$/, function(request, response) {
var query = require("url").parse(request.url, true).query;
if (query.changesSince == null) {
var list = [];
for (var title in talks)
list.push(talks[title]);
sendTalks(list, response);
} else {
var since = Number(query.changesSince);
if (isNaN(since)) {
respond(response, 400, "Invalid parameter");
} else {
var changed = getChangedTalks(since);
if (changed.length > 0)
sendTalks(changed, response);
else
waitForChanges(since, response);
}
}
});
----
When the `changesSince` parameter is missing, the handler simply
builds up a list of all talks, and returns that.
In the other case, the `changeSince` parameter first has to be checked
to make sure that it is a valid number. The `getChangedTalks`
function, to be defined shortly, returns an array of changed talk
since a given point in time. If it returns an empty array, the server
does not yet have anything to send back to the client, so it stores
the response object (using `waitForChanges`) to be responded to at a
later time.
// include_code >code/skillsharing/skillsharing_server.js
[source,javascript]
----
var waiting = [];
function waitForChanges(since, response) {
var waiter = {since: since, response: response};
waiting.push(waiter);
setTimeout(function() {
var found = waiting.indexOf(waiter);
if (found > -1) {
waiting.splice(found, 1);
sendTalks([], response);
}
}, 90 * 1000);
}
----
When a response object is stored in the `waiting` array, a timeout is
immediately set. After 90 seconds, this timeout sees if the request is
still waiting, and if it is, sends an empty response and removes it
from the `waiting` array.
[[registerChange]] To be able to find exactly those talks that have
been changed since a given point in time, we need to keep track of the
history of changes. Registering a change with `registerChange` will
remember that change, along with the current time, in an array called
`changes`. When a change occurs that means there is new data, so all
waiting requests can be responded to immediately.
// include_code >code/skillsharing/skillsharing_server.js
[source,javascript]
----
var changes = [];
function registerChange(title) {
changes.push({title: title, time: Date.now()});
waiting.forEach(function(waiter) {
sendTalks(getChangedTalks(waiter.since), waiter.response);
});
waiting = [];
}
----
Finally, `getChangedTalks` uses the `changes` array to build up an
array of changed talks, including object with a `deleted` property for
talks that no longer exist. When doing this, it has to ensure that it
doesn't include the same talk twice, since there might have been
multiple changes to a talk since the given time.
// include_code >code/skillsharing/skillsharing_server.js
[source,javascript]
----
function getChangedTalks(since) {
var found = [];
function alreadySeen(title) {
return found.some(function(f) {return f.title == title;});
}
for (var i = changes.length - 1; i >= 0; i--) {
var change = changes[i];
if (change.time <= since)
break;
else if (alreadySeen(change.title))
continue;
else if (change.title in talks)
found.push(talks[change.title]);
else
found.push({title: change.title, deleted: true});
}
return found;
}
----
That concludes the server code. Running the program defined so far
will get you a server running on port 8000, which serves up files from
the `public` subdirectory alongside a talk-managing interface under
the `/talks` URL.
== The client ==
The client-side part of the talk-managing website consists of three
files: an HTML page, a style sheet, and a JavaScript file.
=== HTML ===
It is a widely used convention for web servers to try and serve up a
file named `index.html` when a request is made directly to a path that
corresponds to a directory. The file server module we use, `ecstatic`,
supports this convention. When a request is made to the path `/`, it
looks for a file `./public/index.html` (`./public` being the root we
gave it), and returns that if it finds it.
Thus, if we want a page to show up when a browser is pointed at our
server, we should put it in `public/index.html`. This is how our
`index.html` file starts:
// include_code >code/skillsharing/public/index.html
[source,text/html]
----
<!doctype html>
<title>Skill Sharing</title>
<link rel="stylesheet" href="skillsharing.css">
<h1>Skill sharing</h1>
<p>Your name: <input type="text" id="name"></p>
<div id="talks"></div>
----
It defines the document title and includes a style sheet, which
defines a few styles which, among other things, add a border around
talks. Then it adds a heading, and a name field. The user is expected
to put their name in the latter, so that it can be attached to talks
and comments they submit.
The `<div>` element with ID `"talks"` will contain the current list of
talks. The script fills it in when it receives talks from the server.
Next comes the form that is used to create a new talk:
// include_code >code/skillsharing/public/index.html
[source,text/html]
----
<form id="newtalk">
<h3>Submit a talk</h3>
Title: <input type="text" style="width: 40em" name="title">
<br>
Summary: <input type="text" style="width: 40em" name="summary">
<button type="submit">Send</button>
</form>
----
The script will add a `"submit"` event handler to this form, from
which it can make the HTTP request that tells the server about the
talk.
Next comes a rather mysterious block, which has its `display` style
set to `none`, preventing it from actually showing up on the page. Can
you guess what it is for?
// include_code >code/skillsharing/public/index.html
[source,text/html]
----
<div id="template" style="display: none">
<div class="talk">
<h2>{{title}}</h2>
<div>by <span class="name">{{presenter}}</span></div>
<p>{{summary}}</p>
<div class="comments"></div>
<form>
<input type="text" name="comment">
<button type="submit">Add comment</button>
<button type="button" class="del">Delete talk</button>
</form>
</div>
<div class="comment">
<span class="name">{{author}}</span>: {{message}}
</div>
</div>
----
Creating complicated DOM structures with JavaScript code produces ugly
code. You can make it slightly better by introducing helper functions
like the `elt` function from link:13_dom.html#elt[Chapter 13], but the
result will still look worse than HTML, which can be thought of as a
((domain-specific language)) for expressing DOM structures.
To create DOM structures for the talks, our program will define a very
simple _templating_ system, which uses hidden DOM structures included
in the document to instantiate new DOM structures, replacing the
placeholders between double braces with the values of a specific talk.
Finally, the HTML document includes the script file that contains the
client-side code.
// test: never
// include_code >code/skillsharing/public/index.html
[source,text/html]
----
<script src="skillsharing_client.js"></script>
----
=== Starting up ===
The first thing the client has to do when the page is loaded is to ask
the server for the current set of talks. Since we are going to make a
lot of HTTP requests, we will again define a small wrapper around
`XMLHttpRequest`, which accepts an object to configure the request as
well as a callback to call when the request finishes.
// include_code >code/skillsharing/public/skillsharing_client.js
[source,javascript]
----
function request(options, callback) {
var req = new XMLHttpRequest();
req.open(options.method || "GET", options.pathname, true);
req.addEventListener("load", function() {
if (req.status < 400)
callback(null, req.responseText);
else
callback(new Error("Request failed: " + req.statusText));
});
req.addEventListener("error", function() {
callback(new Error("Network error"));
});
req.send(options.body || null);
}
----
The initial request displays the talks it receives on the screen, and
starts the long-polling process by calling `waitForChanges`.
// test: no
// include_code >code/skillsharing/public/skillsharing_client.js
[source,javascript]
----
var lastServerTime = 0;
request({pathname: "talks"}, function(error, response) {
if (error) {
reportError(error);
} else {
response = JSON.parse(response);
displayTalks(response.talks);
lastServerTime = response.serverTime;
waitForChanges();
}
});
----
The `lastServerTime` variable is used to track the time of the last
update that was received from the server. After the initial request,
the client's view of the talks corresponds to the view that the server
had when it responded to that request. Thus, the `serverTime` property
included in the response provides an appropriate initial value for
`lastServerTime`.
When the request fails, we don't want to have our page just sit there,
doing nothing without explanation. So we define a simple function
`reportError`, which at least shows the user a dialog that tells them
something went wrong.
// include_code >code/skillsharing/public/skillsharing_client.js
[source,javascript]
----
function reportError(error) {
if (error)
alert(error.toString());
}
----
The function checks whether there _is_ an actual error, and only
reports it when there is. That way, we can also directly pass it to
`request` for requests where we can ignore the response to make sure
that if the request fails, the error is reported to the user.
=== Displaying talks ===
To be able to update the view of the talks when changes come in, the
client must keep track of the talks that it is currently showing. That
way, when a new version of a talk that is already on the screen comes
in, it can be replaced (in-place) with its updated form. Similarly,
when information comes in that a talk is being deleted, the right DOM
element can be removed from the document.
The function `displayTalks` is used both to build up the initial
display, and to update it when something changes. It will use the
`shownTalks` object, which associates talk titles with DOM nodes, to
remember the talks it currently has on the screen.
// test: no
// include_code >code/skillsharing/public/skillsharing_client.js
[source,javascript]
----
var talkDiv = document.querySelector("#talks");
var shownTalks = Object.create(null);
function displayTalks(talks) {
talks.forEach(function(talk) {
var shown = shownTalks[talk.title];
if (talk.deleted) {
if (shown) {
talkDiv.removeChild(shown);
delete shownTalks[talk.title];
}
} else {
var node = drawTalk(talk);
if (shown)
talkDiv.replaceChild(node, shown);
else
talkDiv.appendChild(node);
shownTalks[talk.title] = node;
}
});
}
----
Building up the DOM structure for talks is done using the templates
that were included in the HTML document. First we must define
`instantiateTemplate`, which looks up and fills in a template.
The `name` parameter is the template's name. To look up the template
element, we search for an element whose class name matches the
template name, which is a child of the element with ID `"template"`.
Using the `querySelector` method, this is easy. There were templates
named `"talk"` and `"comment"` in the HTML page.
// include_code >code/skillsharing/public/skillsharing_client.js
[source,javascript]
----
function instantiateTemplate(name, values) {
function instantiateText(text) {
return text.replace(/\{\{(\w+)\}\}/g, function(_, name) {
return values[name];
});
}
function instantiate(node) {
if (node.nodeType == document.ELEMENT_NODE) {
var copy = node.cloneNode();
for (var i = 0; i < node.childNodes.length; i++)
copy.appendChild(instantiate(node.childNodes[i]));
return copy;
} else if (node.nodeType == document.TEXT_NODE) {
return document.createTextNode(
instantiateText(node.nodeValue));
}
}
var template = document.querySelector("#template ." + name);
return instantiate(template);
}
----
The `cloneNode` method, which all DOM nodes have, creates a copy of a
node. It won't copy the node's child nodes, unless `true` is given as
a first argument. The `instantiate` function recursively builds up a
copy of the template, filling it in as it goes.
The second argument to `instantiateTemplate` should be an object,
whose properties hold the strings that are to be filled into the
template. A placeholder like `{{title}}` will be replaced with the
value of `values`’ `title` property.
This is a crude approach to templating, but it is enough to implement
`drawTalk`.
// include_code >code/skillsharing/public/skillsharing_client.js
[source,javascript]
----
function drawTalk(talk) {
var node = instantiateTemplate("talk", talk);
var comments = node.querySelector(".comments");
talk.comments.forEach(function(comment) {
comments.appendChild(
instantiateTemplate("comment", comment));
});
node.querySelector("button.del").addEventListener(
"click", deleteTalk.bind(null, talk.title));
var form = node.querySelector("form");
form.addEventListener("submit", function(event) {
event.preventDefault();
addComment(talk.title, form.elements.comment.value);
form.reset();
});
return node;
}
----
After instantiating the `"talk"` template, there are various things
that need to be patched up. First, the comments have to be filled in,
by repeatedly instantiating the `"comment"` template, and appending
the results to the node with class `"comments"`. Next, event handlers
have to be attached to the button that deletes the task, and the form
that adds a new comment.
=== Updating the server ===
The event handlers registered by `drawTalk` call the function
`deleteTalk` and `addComment` to perform the actual actions required
to delete a talk or add a comment. These will need to build up URLs
that refer to talks with a given title, for which we define the
following helper function:
// include_code >code/skillsharing/public/skillsharing_client.js
[source,javascript]
----
function talkURL(title) {
return "talks/" + encodeURIComponent(title);
}
----
The `deleteTalk` function is straightforward. It fires off a `DELETE`
request, and reports the error when that fails.
// include_code >code/skillsharing/public/skillsharing_client.js
[source,javascript]
----
function deleteTalk(title) {
request({pathname: talkURL(title), method: "DELETE"},
reportError);