Debian Codegraph @

doc/src/guide/ws_protocol.asciidoc

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