modern_web.asciidoc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. [[modern_web]]
  2. == The modern Web
  3. Let's take a look at various technologies from the beginnings
  4. of the Web up to this day, and get a preview of what's
  5. coming next.
  6. Cowboy is compatible with all the technology cited in this
  7. chapter except of course HTTP/2.0 which has no implementation
  8. in the wild at the time of writing.
  9. === The prehistoric Web
  10. HTTP was initially created to serve HTML pages and only
  11. had the GET method for retrieving them. This initial
  12. version is documented and is sometimes called HTTP/0.9.
  13. HTTP/1.0 defined the GET, HEAD and POST methods, and
  14. was able to send data with POST requests.
  15. HTTP/1.0 works in a very simple way. A TCP connection
  16. is first established to the server. Then a request is
  17. sent. Then the server sends a response back and closes
  18. the connection.
  19. Suffice to say, HTTP/1.0 is not very efficient. Opening
  20. a TCP connection takes some time, and pages containing
  21. many assets load much slower than they could because of
  22. this.
  23. Most improvements done in recent years focused on reducing
  24. this load time and reducing the latency of the requests.
  25. === HTTP/1.1
  26. HTTP/1.1 quickly followed and added a keep-alive mechanism
  27. to allow using the same connection for many requests, as
  28. well as streaming capabilities, allowing an endpoint to send
  29. a body in well defined chunks.
  30. HTTP/1.1 defines the OPTIONS, GET, HEAD, POST, PUT, DELETE,
  31. TRACE and CONNECT methods. The PATCH method was added in more
  32. recent years. It also improves the caching capabilities with
  33. the introduction of many headers.
  34. HTTP/1.1 still works like HTTP/1.0 does, except the connection
  35. can be kept alive for subsequent requests. This however allows
  36. clients to perform what is called as pipelining: sending many
  37. requests in a row, and then processing the responses which will
  38. be received in the same order as the requests.
  39. === REST
  40. The design of HTTP/1.1 was influenced by the REST architectural
  41. style. REST, or REpresentational State Transfer, is a style of
  42. architecture for loosely connected distributed systems.
  43. REST defines constraints that systems must obey to in order to
  44. be RESTful. A system which doesn't follow all the constraints
  45. cannot be considered RESTful.
  46. REST is a client-server architecture with a clean separation
  47. of concerns between the client and the server. They communicate
  48. by referencing resources. Resources can be identified, but
  49. also manipulated. A resource representation has a media type
  50. and information about whether it can be cached and how. Hypermedia
  51. determines how resources are related and how they can be used.
  52. REST is also stateless. All requests contain the complete
  53. information necessary to perform the action.
  54. HTTP/1.1 defines all the methods, headers and semantics required
  55. to implement RESTful systems.
  56. REST is most often used when designing web application APIs
  57. which are generally meant to be used by executable code directly.
  58. === XmlHttpRequest
  59. Also know as AJAX, this technology allows Javascript code running
  60. on a web page to perform asynchronous requests to the server.
  61. This is what started the move from static websites to dynamic
  62. web applications.
  63. XmlHttpRequest still performs HTTP requests under the hood,
  64. and then waits for a response, but the Javascript code can
  65. continue to run until the response arrives. It will then receive
  66. the response through a callback previously defined.
  67. This is of course still requests initiated by the client,
  68. the server still had no way of pushing data to the client
  69. on its own, so new technology appeared to allow that.
  70. === Long-polling
  71. Polling was a technique used to overcome the fact that the server
  72. cannot push data directly to the client. Therefore the client had
  73. to repeatedly create a connection, make a request, get a response,
  74. then try again a few seconds later. This is overly expensive and
  75. adds an additional delay before the client receives the data.
  76. Polling was necessary to implement message queues and other
  77. similar mechanisms, where a user must be informed of something
  78. when it happens, rather than when he refreshes the page next.
  79. A typical example would be a chat application.
  80. Long-polling was created to reduce the server load by creating
  81. less connections, but also to improve latency by getting the
  82. response back to the client as soon as it becomes available
  83. on the server.
  84. Long-polling works in a similar manner to polling, except the
  85. request will not get a response immediately. Instead the server
  86. leaves it open until it has a response to send. After getting
  87. the response, the client creates a new request and gets back
  88. to waiting.
  89. You probably guessed by now that long-polling is a hack, and
  90. like most hacks it can suffer from unforeseen issues, in this
  91. case it doesn't always play well with proxies.
  92. === HTML5
  93. HTML5 is, of course, the HTML version after HTML4. But HTML5
  94. emerged to solve a specific problem: dynamic web applications.
  95. HTML was initially created to write web pages which compose
  96. a website. But soon people and companies wanted to use HTML
  97. to write more and more complex websites, eventually known as
  98. web applications. They are for example your news reader, your
  99. email client in the browser, or your video streaming website.
  100. Because HTML wasn't enough, they started using proprietary
  101. solutions, often implemented using plug-ins. This wasn't
  102. perfect of course, but worked well enough for most people.
  103. However, the needs for a standard solution eventually became
  104. apparent. The browser needed to be able to play media natively.
  105. It needed to be able to draw anything. It needed an efficient
  106. way of streaming events to the server, but also receiving
  107. events from the server.
  108. The solution went on to become HTML5. At the time of writing
  109. it is being standardized.
  110. === EventSource
  111. EventSource, sometimes also called Server-Sent Events, is a
  112. technology allowing servers to push data to HTML5 applications.
  113. EventSource is one-way communication channel from the server
  114. to the client. The client has no means to talk to the server
  115. other than by using HTTP requests.
  116. It consists of a Javascript object allowing setting up an
  117. EventSource connection to the server, and a very small protocol
  118. for sending events to the client on top of the HTTP/1.1
  119. connection.
  120. EventSource is a lightweight solution that only works for
  121. UTF-8 encoded text data. Binary data and text data encoded
  122. differently are not allowed by the protocol. A heavier but
  123. more generic approach can be found in Websocket.
  124. === Websocket
  125. Websocket is a protocol built on top of HTTP/1.1 that provides
  126. a two-ways communication channel between the client and the
  127. server. Communication is asynchronous and can occur concurrently.
  128. It consists of a Javascript object allowing setting up a
  129. Websocket connection to the server, and a binary based
  130. protocol for sending data to the server or the client.
  131. Websocket connections can transfer either UTF-8 encoded text
  132. data or binary data. The protocol also includes support for
  133. implementing a ping/pong mechanism, allowing the server and
  134. the client to have more confidence that the connection is still
  135. alive.
  136. A Websocket connection can be used to transfer any kind of data,
  137. small or big, text or binary. Because of this Websocket is
  138. sometimes used for communication between systems.
  139. === SPDY
  140. SPDY is an attempt to reduce page loading time by opening a
  141. single connection per server, keeping it open for subsequent
  142. requests, and also by compressing the HTTP headers to reduce
  143. the size of requests.
  144. SPDY is compatible with HTTP/1.1 semantics, and is actually
  145. just a different way of performing HTTP requests and responses,
  146. by using binary frames instead of a text-based protocol.
  147. SPDY also allows the server to send extra responses following
  148. a request. This is meant to allow sending the resources
  149. associated with the request before the client requests them,
  150. saving latency when loading websites.
  151. SPDY is an experiment that has proven successful and is used
  152. as the basis for the HTTP/2.0 standard.
  153. Browsers make use of TLS Next Protocol Negotiation to upgrade
  154. to a SPDY connection seamlessly if the protocol supports it.
  155. The protocol itself has a few shortcomings which are being
  156. fixed in HTTP/2.0.
  157. === HTTP/2.0
  158. HTTP/2.0 is the long-awaited update to the HTTP/1.1 protocol.
  159. It is based on SPDY although a lot has been improved at the
  160. time of writing.
  161. HTTP/2.0 is an asynchronous two-ways communication channel
  162. between two endpoints.