ws_protocol.asciidoc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. [[ws_protocol]]
  2. == The Websocket protocol
  3. This chapter explains what Websocket is and why it is
  4. a vital component of soft realtime Web applications.
  5. === Description
  6. Websocket is an extension to HTTP that emulates plain TCP
  7. connections between the client, typically a Web browser,
  8. and the server. It uses the HTTP Upgrade mechanism to
  9. establish the connection.
  10. Websocket connections are fully asynchronous, unlike
  11. HTTP/1.1 (synchronous) and HTTP/2 (asynchronous, but the
  12. server can only initiate streams in response to requests).
  13. With Websocket, the client and the server can both send
  14. frames at any time without any restriction. It is closer
  15. to TCP than any of the HTTP protocols.
  16. Websocket is an IETF standard. Cowboy supports the standard
  17. and all drafts that were previously implemented by browsers,
  18. excluding the initial flawed draft sometimes known as
  19. "version 0".
  20. === Websocket vs HTTP/2
  21. For a few years Websocket was the only way to have a
  22. bidirectional asynchronous connection with the server.
  23. This changed when HTTP/2 was introduced. While HTTP/2
  24. requires the client to first perform a request before
  25. the server can push data, this is only a minor restriction
  26. as the client can do so just as it connects.
  27. Websocket was designed as a kind-of-TCP channel to a
  28. server. It only defines the framing and connection
  29. management and lets the developer implement a protocol
  30. on top of it. For example you could implement IRC over
  31. Websocket and use a Javascript IRC client to speak to
  32. the server.
  33. HTTP/2 on the other hand is just an improvement over
  34. the HTTP/1.1 connection and request/response mechanism.
  35. It has the same semantics as HTTP/1.1.
  36. If all you need is to access an HTTP API, then HTTP/2
  37. should be your first choice. On the other hand, if what
  38. you need is a different protocol, then you can use
  39. Websocket to implement it.
  40. === Implementation
  41. Cowboy implements Websocket as a protocol upgrade. Once the
  42. upgrade is performed from the `init/2` callback, Cowboy
  43. switches to Websocket. Please consult the next chapter for
  44. more information on initiating and handling Websocket
  45. connections.
  46. The implementation of Websocket in Cowboy is validated using
  47. the Autobahn test suite, which is an extensive suite of tests
  48. covering all aspects of the protocol. Cowboy passes the
  49. suite with 100% success, including all optional tests.
  50. Cowboy's Websocket implementation also includes the
  51. permessage-deflate and x-webkit-deflate-frame compression
  52. extensions.
  53. Cowboy will automatically use compression when the
  54. `compress` option is returned from the `init/2` function.