diff --git a/Vik_comments_to_editor.adoc b/Vik_comments_to_editor.adoc index b9791528..32113027 100644 --- a/Vik_comments_to_editor.adoc +++ b/Vik_comments_to_editor.adoc @@ -39,4 +39,17 @@ from +Chapters 10-12.pdf+ document from +Chapters 10-12.pdf+ document * p. 359 inconsistency addressed in a favor of JSHint. -* p. 396 I don't understand what editor meant here. \ No newline at end of file +* p. 396 I don't understand what editor meant here. + +== Chapter 9 + +* p. 403 these tree subsections describe existing architectural styles achieving or emulating same aim - bidirectional communications. On the contrary, websocket is a absolutely new protocol that uses http to bootstrap communication. +* p. 404 explanation follows +* p. 407 SSE does only server-push. But it's only one way street. +* p. 408 We don't need to provide any "advanced introduction to IDL". We provide link to wikipedia. IDL not essential to WebSocket. It's just one of the ways to describe APIs. +* p. 411 Explanation of TCP is out of the scope of this book. In order for readers to understand this section, we can recommend O'reillys book about TCP, HTTP, HTTPs. Magic string is exact (as mentioned). +* p. 412 I'll stick to my formulationю +* p. 418 Yes, csv and psv are still very common formats in enterprises these days +* p. 420 JSON was extensively covered in ch2. DOn't want to repeat it here. +* p. 422 ??? +* p. 439 Chrome Developer Tools are availabe on for ... Chrome. Wireshark allows to debug networking in browsers which development tools lack of debugging tools for websocket. \ No newline at end of file diff --git a/ch9_websockets.asciidoc b/ch9_websockets.asciidoc index 436adea2..97a83192 100644 --- a/ch9_websockets.asciidoc +++ b/ch9_websockets.asciidoc @@ -1,23 +1,36 @@ :imagesdir: ./ -== Upgrading HTTP to WebSocket +== Upgrading HTTP To WebSocket -This chapter is about upgrading from the HTTP protocol to a more responsive HTML5 WebSocket. It starts with a brief overview of the existing legacy Web networking, and then you'll learn why and how to use HTML5's Server-Sent Events (SSE) and WebSockets. +This chapter is about upgrading from the HTTP protocol to a more responsive HTML5 WebSocket. It starts with a brief overview of the existing legacy Web networking, and then you'll learn why and how to use WebSockets. -The new version of our Save The Child application will include the online auction utilizing WebSocket, and you'll see an example of using Server-Sent Events for broadcasting the up-to-the-minute donation information. The goal is to let you see the advantages of changing the way of client-server communications on the Web. You'll clearly see the advantages of WebSocket over a regular HTTP by monitoring the network traffic with such tools as Wireshark and Google Chrome Developer Tools. +We're going to show that WebSocket protocol has literally no overhead compared to HTTP. You might be consider to use WebSocket for developing the following types of applications: -All the server-side functionality supporting this chapter is written in Java, using Java API for WebSocket http://java.net/projects/tyrus[reference implementation], which will be the part of upcoming Java EE 7 specification. We are using the http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/[latest available build of the Glassfish Application Server]. If you don't know Java, just treat this server-side setup as a service that supports WebSocket protocol. For Java developers interested in diving into the server-side, we'll provide the source code and brief comments as a part of the code samples that come with this book. +- Live trading/auctions/sports notifications. +- Live collaborative writing. +- Controlling medical equipment over the web. +- Chat applications. +- Multi-player online games. +- Real-time updates in social streams. + +For the next version of Save The Child application we're going to use WebSocket to implement an online auction communication layer. The goal is to let individuals and businesses purchase hand-made crafts and arts made by children. All proceeds will go to help The Children. + +The goal is to let you see the advantages of changing the way of client-server communications on the Web. You'll clearly see the advantages of WebSocket over a regular HTTP by monitoring the network traffic with such tools as Wireshark and Google Chrome Developer Tools. + +All the server-side functionality supporting this chapter is written in Java, using Java API for WebSocket http://java.net/projects/tyrus[reference implementation], which is a part of Java EE 7 specification. We are using the http://dlc.sun.com.edgesuite.net/glassfish/4.0/release/[latest release of the Glassfish Application Server]. If you don't know Java, just treat this server-side setup as a service that supports WebSocket protocol. For Java developers interested in diving into the server-side, we'll provide the source code and brief comments as a part of the code samples that come with this book. We'll show and compare the server-side data push done with Server-Sent Events and WebSocket. Also you'll see a brief overview of popular existing frameworks that can streamline your WebSocket application development. === Near Real Time Applications With HTTP -The HTTP protocol is the lingua franca of today's Web applications, where client-server communications are based on the request-response paradigm. On the low level, Web browsers establish a TCP/IP connection for each HTTP session. Currently there are 3 basic options that developers use for the browser-server communication: polling, long polling, and streaming. These options are hacks on the top of a half-duplex (a one way street) HTTP protocol to simulate real-time behavior. By _real-time_ we mean the ability to react to some event as it happens. Lets discuss each of them. +The HTTP protocol is the lingua franca of today's Web applications, where client-server communications are based on the request-response paradigm. On the low level, Web browsers establish a TCP/IP connection for each HTTP session. Currently there are 3 basic options that developers use for the browser-server communication: polling, long polling, and streaming. These options are hacks on the top of a half-duplex (a one way street) HTTP protocol to simulate real-time behavior. (By _real-time_ we mean the ability to react to some event as it happens. Lets discuss each of them). ==== Polling ==== -With _polling_, your client code sends requests to the server after based on some pre-configured interval (e.g. using JavaScript `setInterval()` function). Some of the server's responses will be empty, if the requested data is not ready yet as illustrated in <>. For example, if you're running an online auctions and sends the request to see the updated bids, you won't receive any data back unless someone placed a new bid. Visualize a child seating on back seat of your car and asking every minute, "Have we arrived yet?" . And you're politely replying, "Not just yet" - this is similar to an empty server response. There is no valuable payload for this kid, but she's still receiving some "metadata". HTTP polling may result in receiving verbose HTTP response headers bearing no data load, let alone destructing the driver (think the server) from performing other responsibilities. +With _polling_, your client code sends requests to the server after based on some pre-configured interval (e.g. using JavaScript `setInterval()` function). Some of the server's responses will be empty, if the requested data is not ready yet as illustrated in <>. For example, if you're running an online auctions and sends the request to see the updated bids, you won't receive any data back unless someone placed a new bid. + +Visualize a child seating on back seat of your car and asking every minute, "Have we arrived yet?" . And you're politely replying, "Not just yet" - this is similar to an empty server response. There is no valuable payload for this kid, but she's still receiving some "metadata". HTTP polling may result in receiving verbose HTTP response headers bearing no data load, let alone distracting the driver (think the server) from performing other responsibilities. [[FIG9-1]] .Polling @@ -27,9 +40,9 @@ image:images/fig_09_01.png[image] _Long polling_ (see <> ) starts similarly to polling: the client sends the HTTP request to the server. But in this case instead of sending an empty response back, the server waits till the data for the client becomes available. If the requested information is not available within the specified time interval, the server sends an empty response to the client, closes, and re-establishes the connection. -We'll give you one more analogy to compare polling and long polling. Imagine a party at the top floor of a building equipped with a smart elevator that goes up every minute and opens the door just in case if one of the guests wants to go down to smoke a cigarette. If no one enters the elevator, it goes to the ground level and in 60 seconds goes up again. This is the polling scenario. But if this elevator would go up, and waited till someone would actually decide to go down, then we could call it a long polling mode. +We'll give you one more analogy to compare polling and long polling. Imagine a party at the top floor of a building equipped with a smart elevator that goes up every minute and opens the door just in case if one of the guests wants to go down to smoke a cigarette. If no one enters the elevator, it goes to the ground level and in 60 seconds goes up again. This is the polling scenario. But if this elevator would go up, and waited till someone would actually decide to go down (or got tired of waiting), then we could call it a long polling mode. -From the HTTP specification perspective this hack is legit: the long polling mode may seem as if we deal with the slow-responding server. That is why this technique also referred as _Hanging GET_. If you see an online auction that automatically modifies the prices as people bid on items it looks as if the server pushes the data to you. But the chances are, this functionality was implemented using long polling, which is not a real server-side data push, but its emulation. +From the HTTP specification perspective described trick is legitimate: the long polling mode may seem as if we deal with the slow-responding server. That is why this technique also referred as _Hanging GET_. If you see an online auction that automatically modifies the prices as people bid on items it looks as if the server pushes the data to you. But the chances are, this functionality was implemented using long polling, which is not a real server-side data push, but its emulation. [[FIG9-2]] .Long Polling @@ -37,7 +50,7 @@ image:images/fig_09_02.png[image] ==== HTTP Streaming ==== -The client sends the request for data. As soon as the server gets the data ready, it starts streaming (adding more and more data to the response object) without closing the connections. The server pushes the data to the client pretending that the response never ends <>. For example, requesting a video from Youtube.com results in streaming data (frame after frame) without closing the HTTP connection. +The client sends the request for data. As soon as the server gets the data ready, it starts streaming (adding more and more data to the response object) without closing the connections. The server pushes the data to the client pretending that the response never ends. For example, requesting a video from Youtube.com results in streaming data (frame after frame) without closing the HTTP connection. [[FIG9-3]] .HTTP Streaming @@ -65,12 +78,14 @@ The above samples create listeners to subscribe specifically `create` and `updat [source, javascript] ---- -source.onmessage(function(e){ +eventSource.onmessage(function(e){ // process the content of e.data here }); ---- -SSE is a good technique for the use cases when the client doesn't need to send the data to the server. In the above example the server sends two types of custom events `create` and `update` to notify subscribed clients about updated donation data so the active clients can monitor the fund-raising process. You can create as many custom events as needed by the application. +SSE is a good technique for the use cases when the client doesn't need to send the data to the server. A good illustration of such server might be Facebook's New Feed Page. A server can automatically update the client with new data without client's request. + +In the above example the server sends two types of custom events `create` and `update` to notify subscribed clients about updated donation data so the active clients can monitor the fund-raising process. You can create as many custom events as needed by the application. The server sends events as text messages that start with `data:` and end with a pair of new line characters, for example: @@ -91,7 +106,7 @@ Reducing kilobytes of data to 2 bytes is more than "a little more byte efficient source: http://ietf.org/mail-archive/web/hybi/current/msg00784.html ____ -WebSocket is a bi-directional full-duplex socket-based protocol. According to http://tools.ietf.org/html/rfc6455[RFC 6455] - the Internet Engineering Task Force (IETF) standard document - the goal of WebSocket technology is to provide a mechanism for Web applications that need two-way communications with servers. This technology doesn't rely on HTTP hacks or on opening multiple connections using `XMLHttpRequest` or `