-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlecture4.html
1306 lines (1208 loc) · 39.8 KB
/
lecture4.html
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
<!DOCTYPE html>
<!--
Web 2.0, CTU course slides
(cc) 2010-2013 Tomas Vitvar, tomas@vitvar.com
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="course" content="Web 2.0" />
<meta name="lecture" content="Lecture 4" />
<meta name="keywords" content="polling, long-polling, WebSockets, new I/O" />
<link type="text/css" rel="stylesheet" href="css/meta.css">
</link>
<link type="text/css" rel="stylesheet" href="css/ctu-fit.css">
</link>
<link type="text/css" rel="stylesheet" href="humla/lib/core/humla.css">
</link>
<script type="text/javascript" src="humla/lib/humla.js"></script>
<title>Protocols for the Realtime Web</title>
</head>
<body>
<footer>
<p><b>#META_LECTURE#: #TITLE#</b>, <span class="meta_semester" />,
<span class="meta_twitter" />
</p>
<p><b>‒ #SLIDE_NO# ‒</b></p>
</footer>
<div class="slide intro">
<hgroup>
<h1><span class="meta_course" /></h1>
<h2>#META_LECTURE#: #TITLE#</h2>
</hgroup>
<div class="author">
<p class="meta_author" />
<p><span class="meta_email" /> • <span class="meta_twitter" /> •
<span class="meta_web" />
</p>
</div>
<center>
<div class="meta_logo"></div>
</center>
<div class="org">
<p class="meta_org" />
<p><span class="meta_orgfac" /> • <span class="meta_field" />
• <span class="meta_orgweb" /></p>
</div>
<div class="etc">
<div class="text-info">
Modified: #LAST_MODIFIED#<br />
Humla v#HUMLA_VERSION#
</div>
<a href="http://creativecommons.org/licenses/by-sa/3.0/">
<div class="license"></div>
</a>
<div class="oppa"></div>
</div>
</div>
<div class="slide">
<hgroup>
<h1>Overview of APIs and Protocols</h1>
</hgroup>
<style>
.custom_table tbody tr td {
padding-top: 7px;
padding-bottom: 7px
}
</style>
<table class="prettytable custom_table"
style="margin-left: 50px; margin-top: 30px; margin-bottom: 30px; width: 700px">
<caption>APIs and Protocols</caption>
<thead>
<tr>
<th width="200px"></th>
<th>XHR</th>
<th>Fetch API</th>
<th>Server-Sent Events</th>
<th>WebSocket</th>
</tr>
</thead>
<tbody>
<tr>
<td>Request streaming</td>
<td>no</td>
<td>yes</td>
<td>no</td>
<td>yes</td>
</tr>
<tr>
<td>Response streaming</td>
<td>limited</td>
<td>yes</td>
<td>yes</td>
<td>yes</td>
</tr>
<tr>
<td>Framing mechanism</td>
<td>HTTP</td>
<td>HTTP</td>
<td>event stream</td>
<td>binary framing</td>
</tr>
<tr>
<td>Binary data transfers</td>
<td>yes</td>
<td>yes</td>
<td>no (base64)</td>
<td>yes</td>
</tr>
<tr>
<td>Compression</td>
<td>yes</td>
<td>yes</td>
<td>yes</td>
<td>limited</td>
</tr>
<tr>
<td>App. transport protocol</td>
<td>HTTP</td>
<td>HTTP</td>
<td>HTTP</td>
<td>WebSocket</td>
</tr>
<tr>
<td>Net. transport protocol</td>
<td>TCP</td>
<td>TCP</td>
<td>TCP</td>
<td>TCP</td>
</tr>
</tbody>
</table>
<!--
<img style="padding: 10px 0 0 10px" src="img/APIs-protocols.png"></img> -->
</div>
<div class="slide outline"></div>
<section>
<header>Streaming and Long-polling</header>
<div class="slide">
<hgroup>
<h1>Pushing and Polling</h1>
</hgroup>
<div id="1TXuRmtQIG5wJtioixp-dizSX_TguDBJwE5WoYpCyQnE" class="h-drawing" style="height: 200px"></div>
<ul class="xx-small">
<li>Conceptual basis in messaging architectures</li>
<ul>
<li>event-driven architectures (EDA)</li>
</ul>
<li><b>HTTP is a request-response protocol</b></li>
<ul>
<li>response cannot be sent without request</li>
<li>server cannot initiate the communication</li>
</ul>
<li><b>Polling</b> – client periodically checks for updates on the server</li>
<li><b>Pushing</b> – updates from the server (also called COMET)<br />
= <b>long polling</b> – server holds the request for some time<br />
= <b>streaming</b> – server sends updates without closing the socket</li>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>HTTP Streaming</h1>
</hgroup>
<div id="1j6nE63Ed5zVDIgUVJeool8t9mWHtPsrs8e356Wc0b3E" class="h-drawing" style="height: 280px"></div>
<ul class="xx-small">
<li>server deffers the response until an event or timeout is available</li>
<li>when an event is available, server sends it back to client as part of
the response; this does not terminate the connection</li>
<li>server is able to send pieces of response w/o terminating the conn.</li>
<ul>
<li>using <code>transfer-encoding</code> header in HTTP 1.1</li>
<li>using End of File in HTTP 1.0<br />
(server omits <code>content-lenght</code> in the response)</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Chunked Response</h1>
</hgroup>
<ul class="x-small">
<li>Transfer encoding <code>chunked</code></li>
<ul>
<li>It allows to send multiple sets of data over a single connection</li>
<li>a chunk represents data for the event</li>
<pre class="brush: plain">
HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked
25
This is the data in the first chunk
1C
and this is the second one
0</pre>
<li>Each chunk starts with hexadecimal value for length</li>
<li>End of response is marked with the chunk length of 0</li>
</ul>
<li>Steps:</li>
<ul>
<li>server sends HTTP headers and the first chunk (step 3)</li>
<li>server sends second and subsequent chunk of data (step 4)</li>
<li>server terminates the connection (step 5)</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Issues with Chunked Response</h1>
</hgroup>
<ul class="small">
<li>Chunks vs. Events</li>
<ul>
<li>chunks cannot be considered as app messages (events)</li>
<li>intermediaries might "re-chunk" the message stream<br />
→ e.g., combining different chunks into a longer one</li>
</ul>
<li>Client Buffering</li>
<ul>
<li>clients may buffer all data chunks before they make the response
available to the client application</li>
</ul>
<li>HTTP streaming in browsers</li>
<ul>
<li>Server-sent events</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>XHR Polling</h1>
</hgroup>
<ul class="xx-small">
<li>Client is making a period checks</li>
<pre class="brush: javascript">
function checkUpdates(url) {
var xhr = new XHMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() { ... };
xhr.send()
}
setInterval(function() { checkUpdates('/updates', 60000)});</pre>
<ul>
<li>When new data is available, data is returend</li>
<li>When no data is available, the response is empty</li>
<li>Simple to implement but very inefficient</li>
</ul>
<li>Polling is expensive when interval is small</li>
<ul>
<li>Each XHR reqquest is a standalone HTTP request</li>
<li>HTTP incurs ~850 bytes of overhead for request/response headers</li>
<li>For example, 10 000 clients, each polling with 60 seconds interval:</li>
<ul>
<li class="no-bullet"><code>(850 bytes x 8 bits x 10000)/60 seconds = 1.13 Mbps</code></li>
<li class="no-bullet">Server process 167 requests per second at a rate of 1.13 Mbps throughput
</li>
</ul>
<li>Message latency</li>
<ul>
<li>Depends on the interval, maximum is 60 seconds</li>
<li>Decreasing the interval will put more overhead on the server</li>
</ul>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>XHR Long Polling</h1>
</hgroup>
<div id="1-YIqxkEw22GP7dIa0uqKbEpVzuCQt8yeJLMhiFiPGrs" class="h-drawing" style="height: 260px"></div>
<ul class="xx-small">
<li>Server holds long-poll requests</li>
<ul>
<li>server responds when an event or a timeout occurs</li>
<li>saves computing resources at the server as well as network resources</li>
<li>can be applied over HTTP persistent and non-persistent communication</li>
</ul>
<li>Advantages and Issues</li>
<ul>
<li>Better message latency when interval is not constant</li>
<li>concurrent requests processing at the server</li>
<li>Too many messages may result in worse results that XHR polling</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Server-Sent Events</h1>
</hgroup>
<ul class="x-small">
<li>W3C specification</li>
<ul>
<li>part of HTML5 specs, see <span id="w3c-sse" class="h-ref"></span></li>
<li>API to handle HTTP streaming in browsers by using DOM events</li>
<li>transparent to underlying HTTP streaming mechanism</li>
<ul>
<li>can use both chunked messages and EOF</li>
</ul>
<li>same origin policy applies</li>
</ul>
<li><code>EventSource</code> interface</li>
<ul>
<li>event handlers: <code>onopen</code>, <code>onmessage</code>, <code>onerror</code></li>
<li>constructor <code>EventSource(url)</code> – creates and opens the stream</li>
<li>method <code>close()</code> – closes the connection</li>
<li>attribute <code>readyState</code></li>
<ul>
<li><code>CONNECTING</code> – The connection has not yet been established, or it was
closed and the user agent is reconnecting.</li>
<li><code>OPEN</code> – The user agent has an open connection and is dispatching
events as it receives them.</li>
<li><code>CLOSED</code> – The conn. is not open, the user agent is not
reconnecting.</li>
</ul>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>SSE Example</h1>
</hgroup>
<ul class="x-small">
<li>Initiating <code>EventSource</code></li>
<pre class="brush: javascript; class-name: 'tight'">
if (window.EventSource != null) {
var source = new EventSource('your_event_stream.php');
} else {
// Result to xhr polling :(
}</pre>
<li>Defining event handlers</li>
<pre class="brush: javascript">
source.addEventListener('message', function(e) {
// fires when new event occurs, e.data contains the event data
}, false);
source.addEventListener('open', function(e) {
// Connection was opened
}, false);
source.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED) {
// Connection was closed
}
}, false);</pre>
<ul class="small">
<li>when the conn. is closed, the browser reconnects every ~3 seconds</li>
<ul>
<li>can be changed using <code>retry</code> attribute in the message data</li>
</ul>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Event Stream Format</h1>
</hgroup>
<ul class="x-small">
<li>Format</li>
<ul>
<li>response's <code>content-type</code> must be <code>text/event-stream</code></li>
<li>every line starts with <code>data:</code>, event message terminates with 2 <code>\n</code>
chars.</li>
<li>every message may have associated <code>id</code> (is optional)</li>
<pre class="brush: plain">
id: 12345\n
data: first line\n
data: second line\n\n</pre>
</ul>
<li>JSON data in multiple lines of the message</li>
<ul>
<pre class="brush: plain">
data: {\n
data: "msg": "hello world",\n
data: "id": 12345\n
data: }\n\n</pre>
</ul>
<li>Changing the reconnection time</li>
<ul>
<li>default is 3 seconds</li>
<pre class="brush: plain">
retry: 10000\n
data: hello world\n\n</pre>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Auto-reconnect and Tracing</h1>
</hgroup>
<ul class="x-small">
<li>When a connection is dropped</li>
<ul>
<li><code>EventSource</code> will automatically reconnect</li>
<li>It may advertise the last seen message ID</li>
<ul>
<li>The client appends <code>Last-Event-ID</code> header in the reconnect request</li>
</ul>
<li>The stream can be resumed and lost messages can be retransmitted</li>
</ul>
<li>Example</li>
<pre class="brush: plain; class-name: 'tight'">
id: 43
data: ...
=> Request (after connection is dropped)
GET /stream HTTP/1.1
Host: example.com
Accept: text/event-stream
Last-Event-ID: 43
<= Response
HTTP/1.1 200 OK
Content-Type: text/event-stream
Connection: keep-alive
Transfer-Encoding: chunked
id: 44
data: ...</pre>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>SSE Server-side implementation</h1>
</hgroup>
<ul class="x-small">
<li>Node.js server</li>
<pre class="brush: javascript">
const express = require('express')
const app = express()
app.get('/countdown', function (req, res) {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
})
countdown(res, 10)
})
function countdown(res, count) {
res.write("data: " + count + "\n\n")
if (count)
setTimeout(() => countdown(res, count - 1), 1000)
else
res.end()
}
app.listen(3000, () => console.log('SSE app on http://127.0.0.1:3000'))</pre>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>SSE Server-side implementation - output</h1>
</hgroup>
<ul class="x-small">
<li>Node.js server</li>
<pre class="brush: bash">
$ curl -vvv http://127.0.0.1:3000/countdown
* Trying 127.0.0.1:3000...
* Connected to 127.0.0.1 (127.0.0.1) port 3000
> GET /countdown HTTP/1.1
> Host: 127.0.0.1:3000
> User-Agent: curl/8.4.0
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: text/event-stream
< Cache-Control: no-cache
< Connection: keep-alive
< Date: Mon, 25 Mar 2024 08:32:57 GMT
< Transfer-Encoding: chunked
<
data: 10
data: 9
...</ul>
</div>
<div class="slide">
<hgroup>
<h1>Streams API</h1>
</hgroup>
<ul class="small">
<li>JavaScript API to programmatically access streams of data</li>
<ul>
<li>Before, the whole resource had to be read</li>
<li>Now, you can process raw data as soon as it is available</li>
<ul>
<li>No need to generate string or buffer</li>
<li>Detect streams start and end</li>
<li>Chain streams together</li>
<li>Handle errors, cancel streams</li>
</ul>
</ul>
<li>Streams usage</li>
<ul>
<li>Responses can be available as streams using Fetch API</li>
<ul>
<li>Response body returned by a <code>fetch</code> request and exposed as
<code>ReadableStream</code>
</li>
<li>You can read it using <code>ReadableStream.getReader()</code></li>
<li>You can cancel it using <code>ReadableStream.cancel()</code></li>
</ul>
</ul>
</ul>
</div>
<!--<div class="slide">
<hgroup>
<h1>Demo</h1>
</hgroup>
<ul>
<li>Example Server-sent events demo</li>
<ul class="small" style="zoom: 0.9; margin-bottom: 20px">
<li>requires a server running at <code style="zoom: 0.9">http://demo-sse.vitvar.com:8080</code></li>
<li>runs in <code>iframe</code>, same origin policy applies</li>
<li>call <code>http://demo-sse.vitvar.com:8080/echo?msg=text</code>,<br/>
→ the message should be displayed as event</li>
</ul>
<input id="btn" type="button" onclick="javascript:run_stop_demo()" value="Run Demo"></input>
<input type="button" onclick="javascript:generateEvent()" value="Generate Event"></input>
<input id="event" type="text" value="sample text" style="width: 300px"></input><br/>
<script>
var demoURL = "http://demo-sse.vitvar.com:8080";
function run_stop_demo() {
fr = document.getElementById("if-sse");
bt = document.getElementById("btn");
if (bt.value == "Run Demo") {
fr.src = demoURL;
bt.value = "Stop Demo";
} else {
fr.src = "";
bt.value = "Run Demo";
}
}
function generateEvent() {
fr = document.getElementById("if-gev");
fr.src = demoURL + "/echo?msg=" + document.getElementById("event").value;
}
</script>
<iframe id="if-sse" style="width: 620px; margin-top: 10px; height: 270px;
border: 1px solid silver"></iframe>
<iframe id="if-gev" style="display: none"></iframe>
</div>-->
<div class="slide">
<hgroup>
<h1>Other Technologies</h1>
</hgroup>
<ul class="x-small">
<li>Cross-document messaging</li>
<div id="1DRm9FfN_CyiaYCccFkFOrcygsrwF_cpy7gKg6gEscEU" style="height: 165px" class="h-drawing"></div>
<ul style="margin-top: 10px">
<li>The use of Cross Document Messaging for streaming</li>
<ul class="no-bullet">
<li>1. The client loads a streaming resource in a hidden <code>iframe</code></li>
<li>2. The server pushes a JavaScript code to the <code>iframe</code></li>
<li>3. The browser executes the code as it arrives from the server</li>
<li>4. The embedded iframe's code posts a message to the upper document</li>
</ul>
</ul>
</ul>
</div>
</section>
<div class="slide outline"></div>
<section>
<header>WebSocket Protocol</header>
<div class="slide">
<hgroup>
<h1>WebSocket</h1>
</hgroup>
<ul class="xx-small">
<li>Specifications</li>
<ul>
<li>IETF defines <span id="websocket" class="h-ref"></span></li>
<li>W3C defines <span id="websocket-w3c" class="h-ref"></span></li>
</ul>
<li>Design principles</li>
<ul>
<li>a new protocol</li>
<ul>
<li>browsers, web servers, and proxy servers need to support it</li>
</ul>
<li>a layer on top of TCP</li>
<li>bi-directional communication between client and servers</li>
<ul>
<li>low-latency apps without HTTP overhead</li>
</ul>
<li>Web origin-based security model for browsers</li>
<ul>
<li>same origin policy, cross-origin resource sharing</li>
</ul>
<li>support multiple server-side endpoints</li>
</ul>
<li>Custom URL scheme: <code>ws</code> and <code>wss</code> (TCP and TLS)</li>
<ul>
<li>WebSocket can be used over non-HTTP connections outside of browsers</li>
</ul>
<li>Options to establish a WebSocket connection</li>
<ul>
<li>TLS and ALPN</li>
<li>HTTP/1.1 connection upgrade</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Connection Upgrade – Request</h1>
</hgroup>
<ul class="small">
<li>Request</li>
<ul>
<li>client sends a following HTTP request to upgrade the connection to WebSocket</li>
<pre class="brush: plain">
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Origin: http://example.com
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 7</pre>
<li><code>Connection</code> – request to upgrade the protocol</li>
<li><code>Upgrade</code> – protocol to upgrade to</li>
<li><code>Sec-WebSocket-Key</code> – a client key for later validation</li>
<li><code>Sec-WebSocket-Origin</code> – origin of the request</li>
<li><code>Sec-WebSocket-Protocol</code> – list of sub-protocols that client supports
(proprietary)</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Connection Upgrade – Response</h1>
</hgroup>
<ul class="xx-small">
<li>Response</li>
<ul>
<li>server accepts the request and responds as follows</li>
<pre class="brush: plain">
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol: chat</pre>
<ul>
<li><code>101 Switching Protocols</code> – status code for a successful upgrade</li>
<li><code>Sec-WebSocket-Protocol</code> – a sub-protocol that the server selected
from the list of protocols in the request</li>
<li><code>Sec-WebSocket-Accept</code> – a key to prove it has received a client
WebSocket handshake request</li>
</ul>
<li>Formula to compute <code>Sec-WebSocket-Accept</code></li>
<pre class="brush: plain">
Sec-WebSocket-Accept = Base64Encode(SHA-1(Sec-WebSocket-Key +
"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))</pre>
<ul>
<li><code>SHA-1</code> – hashing function</li>
<li><code>Base64Encode</code> – Base64 encoding function</li>
<li><code>"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"</code> – magic number</li>
</ul>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Data Transfer</h1>
</hgroup>
<ul class="xx-small">
<li>After a successful connection upgrade</li>
<ul>
<li>socket between the client and the "resource" at the server is established</li>
<li>client and the server can both read and write from/to the socket</li>
<li>No HTTP headers overhead</li>
</ul>
<li>Data Framing</li>
<ul>
<li>Data transmitted in TCP packets (see RFC6455: <span class="h-ref" id="ietf-ws-frame"></span>)
</li>
<li>Contains payload length, closing frame, ping, pong, type of data (text/binary), etc. and payload
(message data)</li>
<div style="zoom: 0.85">
<pre class="brush: plain; class-name: tight; gutter: false;">
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+
</pre>
</div>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Frame and Message</h1>
</hgroup>
<ul class="xx-small">
<li>Frame</li>
<ul>
<li>The smallest unit of communication, composed of a header and a paylod</li>
<li>Payload carries all or part of a message</li>
</ul>
<li>Message</li>
<ul>
<li>A sequence of frames that map to a logical application message</li>
<li>Messages decomposition into frames is done by client and server framing code</li>
<li>Application is unaware of frames but only application messages</li>
</ul>
<li>Frame on-the-wire</li>
<ul>
<li><code>FIN</code> – indication whether the frame is final in the message</li>
<li><code>opcode</code> – type of frame: (text, binary, control frame, ping, pong)</li>
<li>Mask bit indicates if the payload is masked (from client to server only)</li>
<li>Payload length</li>
<li>Masking key contains 32-bit key to mask the payload (if mask bit is set)</li>
<li>Payload application data</li>
</ul>
</ul>
</div>
<!-- https://tools.ietf.org/html/rfc6455#section-10.3 -->
<!-- <div class="slide">
<hgroup>
<h1>Attacks on Infrastructure</h1>
</hgroup>
<ul class="xx-small">
<li>Proxy cache poisoning</li>
<ul>
<li>Client establishes a connection to a server under an attacker's control</li>
<li>Client sends <code>upgrade</code> on the HTTP connection</li>
<li>Client sends data over upgraded connection that look like HTTP GET</li>
<ul>
<li>GET for a specific known resource</li>
</ul>
<li>Server responds with resource</li>
<ul>
<li>The resource would be a malicious script</li>
</ul>
<li>The resource is cached by one or more intermediaries, thus poisoning the cache</li>
<li>Attacker can run malicious script on other origins</li>
</ul>
<li>The data must be masked from client to the server</li>
<ul>
<li>Attacker does not have control on how the data being sent appears on the wire</li>
<li>Attacker cannot construct a message that could be misinterpreted by an intermediary as an HTTP request</li>
</ul>
<li>Clients must chose masking key for each frame</li>
<ul>
<li>The key cannot be predicted by end application that provide the data</li>
</ul>
</ul>
</div> -->
<div class="slide">
<hgroup>
<h1>Head-of-line Blocking</h1>
</hgroup>
<ul class="xx-small">
<li>Head-of-line blocking recall</li>
<ul>
<li>Second request needs to wait for the first request to finish</li>
<li>See <a href="https://mdw.vitvar.com/lecture3.html#/18/v1">HTTP Pipelining in AM1</a> for more
details</li>
</ul>
<li>WebSocket</li>
<ul>
<li>Large messages can cause the head of line blocking on the client</li>
<li>Messages can be split into more frames</li>
<li>But frames cannot be interleaved</li>
</ul>
<li class="no-bullet">⇒ A large message with more frames may block frames of other messages</li>
<li>You need to be careful of payload size of each message</li>
<ul>
<li>You should:</li>
<ul>
<li>split large message into multiple application messages</li>
<li>monitor amount of messages in the client's buffer</li>
<li>send data when buffer is empty</li>
</ul>
</ul>
<li>Each WebSocket connection requires a dedicated TCP connection</li>
<ul>
<li>Problem with HTTP/1.x due to a restricted number of connections per origin</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>WebSocket Browser API</h1>
</hgroup>
<ul class="xx-small">
<li>Client-side API</li>
<ul>
<li>clients to utilize WebSocket, supported by all modern browsers</li>
<li>Hides complexity of WebSocket protocol for the developer</li>
</ul>
<li>JavaScript example</li>
<pre class="brush: javascript; class-name: tight">
// ws is a new URL schema for WebSocket protocol; 'chat' is a sub-protocol
var connection = new WebSocket('ws://server.example.org/chat', 'chat');
// When the connection is open, send some data to the server
connection.onopen = function () {
// connection.protocol contains sub-protocol selected by the server
console.log('subprotocol is: ' + connection.protocol);
connection.send('data');
};
// Log errors
connection.onerror = function (error) {
console.log('WebSocket Error ' + error);
};
// Log messages from the server
connection.onmessage = function (e) {
console.log('Server: ' + e.data);
};
...
// closes the connection
connection.close()</pre>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>Avoid Head of Line Blocking</h1>
</hgroup>
<ul class="xx-small">
<li>Example code to monitor a client buffer size</li>
<pre class="brush: javascript; class-name: tight">
var ws = new WebSocket('wss://example.com/socket');
ws.onopen = function () {
subscribeToApplicationUpdates(function(evt) {
if (ws.bufferedAmount == 0)
ws.send(evt.data);
});
};
</pre>
<ul>
<li>A call to <code>send</code> is asynchronous</li>
<li>Multiple calls to <code>send</code> will fill the client buffer</li>
</ul>
</ul>
</div>
<div class="slide">
<hgroup>
<h1>WebSocket Infrastructure</h1>
</hgroup>
<ul class="xx-small">
<li>HTTP optimized for short transfers</li>
<ul>
<li>intermediaries are configured to timeout idle HTTP connections quickly</li>
<li>This can be a problem for long-lived WebSocket connections</li>
</ul>
<li>We cannot control</li>
<ul>
<li>Client's network, some networks may block WebSocket traffic</li>
<ul>
<li>We need a fallback strategy</li>
</ul>
<li>Proxies on the external network</li>
<ul>
<li>TLS may help, tunneling over secure end-to-end connection, WebSocket traffic can bypass
intermediaries</li>
</ul>
</ul>
<li>We can control our infrastructure</li>
<ul>
<li>For example, set tunnel timeout to 1 hour on HAProxy</li>
<pre class="brush: plain">
defaults http
timeout connect 30s
timeout client 30s
timeout server 30s
timeout tunnel 1h</pre>
</ul>
</ul>
</div>
<!-- <div class="slide">
<hgroup>
<h1>Sockets.IO</h1>
</hgroup>
<ul class="x-small spacing">
<li>Many options for streaming</li>
<ul>
<li>long-polling, streaming, iframe, WebSockets</li>
<li>Not all browsers support WebSockets</li>
<li><span id="socket.io" class="h-ref"></span> – a layer providing a unified API</li>
</ul>
<li>Sockets.IO</li>
<ul>
<li>API and JavaScript implementation</li>
<li>checks the availability of WebSocket protocol</li>
<ul>
<li>fallback to long-polling or other technologies when not available</li>
</ul>
</ul>
<pre class="brush: javascript">
// creates a new socket
var socket = new io.Socket();
// event handlers
socket.on('connect', function(){
socket.send('hi!');
})
socket.on('message', function(data){
alert(data);
})
socket.on('disconnect', function(){})</pre>
</ul>
</div> -->
</section>
<!-- <div class="slide outline"></div>
<section>
<header>M-JPEG</header>
<div class="slide">
<hgroup>
<h1>Streaming video</h1>
</hgroup>
<ul class="x-small">
<li>Webcams, IP or USB</li>
<ul>
<li>Play video stream using RTSP or M-JPEG</li>
<li>RTSP (Realtime Streaming Protocol) defines sequences to control palying multimedia</li>
</ul>
<li>Sample tasks</li>
<ul>
<li>Add video stream to a web page</li>
<ul>
<li><code>video</code> HTML5 element</li>
</ul>
<li>Capture frames from the camera and process them</li>
<ul>
<li>Capture frames in a specific format such as JPG</li>