| 1 |
Write an extension |
| 2 |
================== |
| 3 |
|
| 4 |
.. currentmodule:: websockets |
| 5 |
|
| 6 |
During the opening handshake, WebSocket clients and servers negotiate which |
| 7 |
extensions_ will be used and with which parameters. |
| 8 |
|
| 9 |
.. _extensions: https://datatracker.ietf.org/doc/html/rfc6455.html#section-9 |
| 10 |
|
| 11 |
Then, each frame is processed before being sent and after being received |
| 12 |
according to the extensions that were negotiated. |
| 13 |
|
| 14 |
Writing an extension requires implementing at least two classes, an extension |
| 15 |
factory and an extension. They inherit from base classes provided by websockets. |
| 16 |
|
| 17 |
Extension factory |
| 18 |
----------------- |
| 19 |
|
| 20 |
An extension factory negotiates parameters and instantiates the extension. |
| 21 |
|
| 22 |
Clients and servers require separate extension factories with distinct APIs. |
| 23 |
Base classes are :class:`~extensions.ClientExtensionFactory` and |
| 24 |
:class:`~extensions.ServerExtensionFactory`. |
| 25 |
|
| 26 |
Extension factories are the public API of an extension. Extensions are enabled |
| 27 |
with the ``extensions`` parameter of :func:`~asyncio.client.connect` or |
| 28 |
:func:`~asyncio.server.serve`. |
| 29 |
|
| 30 |
Extension |
| 31 |
--------- |
| 32 |
|
| 33 |
An extension decodes incoming frames and encodes outgoing frames. |
| 34 |
|
| 35 |
If the extension is symmetrical, clients and servers can use the same class. The |
| 36 |
base class is :class:`~extensions.Extension`. |
| 37 |
|
| 38 |
Since extensions are initialized by extension factories, they don't need to be |
| 39 |
part of the public API of an extension. |