Write an extension¶
During the opening handshake, WebSocket clients and servers negotiate which extensions will be used and with which parameters.
Then, each frame is processed before being sent and after being received according to the extensions that were negotiated.
Writing an extension requires implementing at least two classes, an extension factory and an extension. They inherit from base classes provided by websockets.
Extension factory¶
An extension factory negotiates parameters and instantiates the extension.
Clients and servers require separate extension factories with distinct APIs.
Base classes are ClientExtensionFactory
and
ServerExtensionFactory
.
Extension factories are the public API of an extension. Extensions are enabled
with the extensions
parameter of connect()
or
serve()
.
Extension¶
An extension decodes incoming frames and encodes outgoing frames.
If the extension is symmetrical, clients and servers can use the same class. The
base class is Extension
.
Since extensions are initialized by extension factories, they don’t need to be part of the public API of an extension.