forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
21_skillsharing.txt
1342 lines (1102 loc) · 48.5 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
:code_links: ["code/skillsharing.zip"]
= Project: Skill-Sharing Website =
(((skill-sharing project)))(((meetup)))(((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.
(((learning)))(((users’ group)))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 goal is to set up a ((website))
for managing ((talk))s given at a skill-sharing meeting. Imagine a
small group of people meeting up regularly in a member’s
office to talk about ((unicycling)). The problem is that 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"]
(!interactive 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 ((download))ed from
http://eloquentjavascript.net/code/skillsharing.zip[_eloquentjavascript.net/code/skillsharing.zip_].
== Design ==
(((skill-sharing project)))(((persistence)))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 ((talk))s 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 ((comment))s associated with it. The
client allows users to propose new talks (adding them to the list),
delete talks, and 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"]
(((live view)))(((user experience)))(((pushing
data)))(((connection)))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.
(((Node.js)))A common solution to this problem is called _((long
polling))_, which happens to be one of the motivations for Node's
design.
== Long polling ==
(((firewall)))(((router)))(((notification)))(((long polling)))To be
able to immediately notify a client that something changed, we need a
((connection)) to that client. Since web ((browser))s 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 do so.
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 ((connection))s 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 continuously ask the server for new information
using regular HTTP requests, and the server simply stalls its answer
when it has nothing new to report.
(((live view)))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
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.
(((robustness)))(((timeout)))To prevent connections from timing out
(being aborted because of 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.
(((Node.js)))A busy server that is using long polling may have
thousands of waiting requests, 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 ==
(((skill-sharing project)))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.
(((path,URL)))We will base our interface on ((JSON)), and like in the file server
from link:20_node.html#file_server[Chapter 20], we'll try to make good use
of HTTP ((method))s. The interface is centered around the `/talks` path.
Paths that do not start with `/talks` will be used for
serving ((static file))s—the HTML and JavaScript code that implements
the client-side system.
(((GET method)))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 return to it
link:21_skillsharing.html#poll_time[later].
(((PUT method)))(((URL)))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.
(((encodeURIComponent function)))(((escaping,in
URLs)))(((whitespace)))Since talk titles may contain spaces and other
characters that may not appear normally in a URL, title strings must be encoded
with the `encodeURIComponent` function when building up such a URL.
[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.
(((POST method)))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?"}
----
(((query string)))(((timeout)))To support ((long polling)), `GET`
requests to `/talks` may include a query parameter called `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 there aren't, the response is
delayed until something happens or until a given time period (we will use
90 seconds) has elapsed.
[[poll_time]]
(((Unix time)))(((Date.now function)))(((synchronization)))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()`. 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 would be impossible for the client to know the
precise time at which the server sent a response because
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`. That property tells the client the
precise time, from the server's perspective, at which the data it
receives was created. The client 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": 1405438913401,
"talks": [{"title": "Unituning",
"deleted": true}]}
----
When a talk has been changed, has been newly created, or has a comment added,
the full representation of the talk is included in the response to
the client's next polling 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.
(((security)))The protocol described in this chapter does not do any
((access control)). Everybody can comment, modify talks, and even
delete them. Since the Internet is filled with ((hooligan))s, putting
such a system online without further protection is likely to end in
disaster.
(((authorization)))(((forwarding)))A simple solution would be to put
the system behind a _((reverse proxy))_, which is an HTTP server that
accepts connections from outside the system and forwards them to HTTP
servers that are running locally. Such a ((proxy)) can be configured
to require a username and password, and you could make sure only the
participants in the skill-sharing group have this ((password)).
== The server ==
(((skill-sharing project)))Let's start by writing the ((server))-side
part of the program. The code in this section runs on ((Node.js)).
=== Routing ===
(((createServer function)))(((path,URL)))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.
(((dispatching)))A _((router))_ is a component that helps dispatch a
request to the function that can handle it. You can tell the router, for
example, that `PUT` requests with a path that
matches the regular expression `/^\/talks\/([^\/]+)$/` (which matches `/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.
(((require function)))(((Router type)))(((module)))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;
});
};
----
(((Router type)))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.
(((some method)))The latter will return a Boolean that indicates
whether a handler was found. 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.
(((capture group)))(((decodeURIComponent function)))(((escaping,in
URLs)))The handler functions are called with the `request` and
`response` objects. When the ((regular expression)) that matches the
URL contains any groups, the strings they match are passed to the handler
as extra arguments. These strings have to be URL-decoded since the raw URL
contains `%20`-style codes.
=== Serving files ===
When a request matches none of the request types defined in our
router, the server must interpret it as 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 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. So let's use a solid, well-tested
((static file)) server from ((NPM)) instead.
(((createServer function)))(((ecstatic module)))I opted for
`ecstatic`. This isn't the only such server on NPM, but it 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 serves _only_ 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);
----
(((JSON)))The `respond` and `respondJSON` helper functions are used throughout the
server code 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 ((talk))s that have been proposed in an object
called `talks`, whose property names are the talk titles. These will
be exposed as HTTP ((resource))s 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.
(((GET method)))(((404 (HTTP status code))))The handler for requests
that `GET` a single talk must look up the talk and respond either with
the talk's JSON data or with a 404 error response.
// 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");
});
----
(((DELETE method)))Deleting a talk is done by removing it from the
`talks` object.
// 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);
});
----
(((long polling)))(((registerChange function)))The `registerChange` function, which we
will define link:21_skillsharing.html#registerChange[later], notifies
waiting long-polling requests about the change.
(((readStreamAsJSON function)))(((body (HTTP))))To retrieve
the content of ((JSON))-encoded request bodies, we define a
function called `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);
});
}
----
(((validation)))(((input)))(((PUT method)))One handler that
needs to read JSON responses is the `PUT` handler, which is used to create new
((talk))s. It has to check 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.
(((registerChange function)))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);
}
});
});
----
(((validation)))(((readStreamAsJSON function)))Adding a ((comment)) to
a ((talk)) works similarly. 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");
}
});
});
----
(((404 (HTTP status code))))Trying to add a comment to a nonexistent
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
be either a simple request for all talks or a request for
updates, with a `changesSince` parameter.
There will 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
});
}
----
(((query string)))(((url module)))(((parsing)))The handler itself
needs to look at the query parameters in the request's URL to see
whether a `changesSince` parameter is given. If you give the `"url"` module's
`parse` function a second argument of `true`, it will
also parse the query part of a URL. The object it returns will have a
`query` property, which holds 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.
(((long polling)))(((validation)))Otherwise, 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 talks 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);
}
----
(((splice method)))(((array,methods)))(((array,indexing)))(((indexOf
method)))The `splice` method is used to cut a piece out of an array.
You give it an index and a number of elements, and it _mutates_ the
array, removing that many elements after the given index. In this
case, we remove a single element, the object that tracks the waiting
response, whose index we found by calling `indexOf`. If you pass
additional arguments to `splice`, their values will be inserted into
the array at the given position, replacing the removed elements.
(((setTimeout function)))(((timeout)))When a response object is stored
in the `waiting` array, a timeout is immediately set. After 90
seconds, this timeout sees whether the request is still waiting and, if it
is, sends an empty response and removes it from the `waiting` array.
[[registerChange]]
(((registerChange function)))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 objects with a `deleted` property for
talks that no longer exist. When building that array, `getChangedTalks` 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 files from
the `public` subdirectory alongside a talk-managing interface under
the `/talks` URL.
== The client ==
(((skill-sharing project)))The ((client))-side part of the
talk-managing website consists of three files: an HTML page, a style
sheet, and a JavaScript file.
=== HTML ===
(((index.html)))It is a widely used convention for web servers to try
to serve 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 `/`, the server looks for the file `./public/index.html` (`./public`
being the root we gave it) and returns that file if found.
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
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 to, 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.
(((id attribute)))(((initialization)))The `<div>` element with the ID
`"talks"` will contain the current list of talks. The script fills the list
in when it receives talks from the server.
(((form (HTML tag))))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>
----
(((submit event)))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.
(((display (CSS))))(((hidden element)))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>
----
(((elt function)))Creating complicated ((DOM)) structures with
JavaScript code produces ugly code. You can make the code 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.
(((DOM,construction)))(((template)))To create DOM structures for the
talks, our program will define a simple _templating_ system,
which uses hidden DOM structures included in the document to
instantiate new DOM structures, replacing the ((placeholder))s between
double braces with the values of a specific talk.
(((script (HTML tag))))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 ===
(((initialization)))(((XMLHttpRequest)))The first thing the client has
to do when the page is loaded is 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);
}
----
(((long polling)))The initial request ((display))s 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();
}
});
----
(((synchronization)))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`.
(((error handling)))(((user experience)))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 called `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());
}
----
(((callback function)))The function checks whether there _is_ an
actual error, and it alerts only when there is one. That way, we can also
directly pass this function to `request` for requests where we can ignore the
response. This makes sure that if the request fails, the error is reported
to the user.
=== Displaying talks ===
(((synchronization)))(((live view)))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, the talk 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;
}
});
}
----
(((drawTalk function)))(((instantiation)))Building up the DOM
structure for talks is done using the ((template))s that were included
in the HTML document. First, we must define `instantiateTemplate`,
which looks up and fills in a template.
(((class attribute)))(((querySelector method)))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
makes this 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));
} else {
return node;
}
}
var template = document.querySelector("#template ." + name);
return instantiate(template);
}
----
(((copying)))(((recursion)))(((cloneNode method)))(((cloning)))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 in the template 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.
(((drawTalk function)))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));
});