Skip to content

Commit 2080026

Browse files
committed
Rewrite extensions guide.
1 parent c000260 commit 2080026

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

docs/extensions.rst

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,99 @@
11
Extensions
22
==========
33

4-
.. currentmodule:: websockets
4+
.. currentmodule:: websockets.extensions
55

66
The WebSocket protocol supports extensions_.
77

8-
At the time of writing, there's only one `registered extension`_, WebSocket
9-
Per-Message Deflate, specified in :rfc:`7692`.
8+
At the time of writing, there's only one `registered extension`_ with a public
9+
specification, WebSocket Per-Message Deflate, specified in :rfc:`7692`.
1010

1111
.. _extensions: https://tools.ietf.org/html/rfc6455#section-9
1212
.. _registered extension: https://www.iana.org/assignments/websocket/websocket.xhtml#extension-name
1313

1414
Per-Message Deflate
1515
-------------------
1616

17-
:func:`~legacy.server.serve()` and :func:`~legacy.client.connect` enable the
18-
Per-Message Deflate extension by default. You can disable this with
19-
``compression=None``.
17+
:func:`~websockets.legacy.client.connect` and
18+
:func:`~websockets.legacy.server.serve` enable the Per-Message Deflate
19+
extension by default.
20+
21+
If you want to disable it, set ``compression=None``::
22+
23+
import websockets
24+
25+
websockets.connect(..., compression=None)
26+
27+
websockets.serve(..., compression=None)
2028

21-
You can also configure the Per-Message Deflate extension explicitly if you
22-
want to customize its parameters.
2329

2430
.. _per-message-deflate-configuration-example:
2531

26-
Here's an example on the server side::
32+
You can also configure the Per-Message Deflate extension explicitly if you
33+
want to customize compression settings::
2734

2835
import websockets
2936
from websockets.extensions import permessage_deflate
3037

31-
websockets.serve(
38+
websockets.connect(
3239
...,
3340
extensions=[
34-
permessage_deflate.ServerPerMessageDeflateFactory(
41+
permessage_deflate.ClientPerMessageDeflateFactory(
3542
server_max_window_bits=11,
3643
client_max_window_bits=11,
3744
compress_settings={'memLevel': 4},
3845
),
3946
],
4047
)
4148

42-
Here's an example on the client side::
43-
44-
import websockets
45-
from websockets.extensions import permessage_deflate
46-
47-
websockets.connect(
49+
websockets.serve(
4850
...,
4951
extensions=[
50-
permessage_deflate.ClientPerMessageDeflateFactory(
52+
permessage_deflate.ServerPerMessageDeflateFactory(
5153
server_max_window_bits=11,
5254
client_max_window_bits=11,
5355
compress_settings={'memLevel': 4},
5456
),
5557
],
5658
)
5759

60+
The window bits and memory level values chosen in these examples reduce memory
61+
usage. You can read more about :ref:`optimizing compression settings
62+
<compression-settings>`.
63+
5864
Refer to the API documentation of
59-
:class:`~extensions.permessage_deflate.ServerPerMessageDeflateFactory` and
60-
:class:`~extensions.permessage_deflate.ClientPerMessageDeflateFactory` for
61-
details.
65+
:class:`~permessage_deflate.ClientPerMessageDeflateFactory` and
66+
:class:`~permessage_deflate.ServerPerMessageDeflateFactory` for details.
6267

6368
Writing an extension
6469
--------------------
6570

6671
During the opening handshake, WebSocket clients and servers negotiate which
6772
extensions will be used with which parameters. Then each frame is processed by
68-
extensions before it's sent and after it's received.
73+
extensions before being sent or after being received.
74+
75+
As a consequence, writing an extension requires implementing several classes:
76+
77+
* Extension Factory: it negotiates parameters and instantiates the extension.
6978

70-
As a consequence writing an extension requires implementing several classes:
79+
Clients and servers require separate extension factories with distinct APIs.
7180

72-
1. Extension Factory: it negotiates parameters and instantiates the extension.
73-
Clients and servers require separate extension factories with distinct APIs.
81+
Extension factories are the public API of an extension.
7482

75-
2. Extension: it decodes incoming frames and encodes outgoing frames. If the
76-
extension is symmetrical, clients and servers can use the same class.
83+
* Extension: it decodes incoming frames and encodes outgoing frames.
84+
85+
If the extension is symmetrical, clients and servers can use the same
86+
class.
87+
88+
Extensions are initialized by extension factories, so they don't need to be
89+
part of the public API of an extension.
7790

7891
``websockets`` provides abstract base classes for extension factories and
79-
extensions.
92+
extensions. See the API documentation for details on their methods:
93+
94+
* :class:`~base.ClientExtensionFactory` and
95+
:class:`~base.ServerExtensionFactory` for extension factories,
8096

81-
.. autoclass:: websockets.extensions.base.ServerExtensionFactory
82-
:members:
97+
* :class:`~base.Extension` for extensions.
8398

84-
.. autoclass:: websockets.extensions.base.ClientExtensionFactory
85-
:members:
8699

87-
.. autoclass:: websockets.extensions.base.Extension
88-
:members:

0 commit comments

Comments
 (0)