Skip to content

Commit 5713ac7

Browse files
authored
DOC Add documentation on websockets level ping frames (#345)
1 parent 05c05a2 commit 5713ac7

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

docs/transports/websockets.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ There are two ways to send authentication tokens with websockets depending on th
8282
init_payload={'Authorization': 'token'}
8383
)
8484
85+
.. _websockets_transport_keepalives:
86+
8587
Keep-Alives
8688
-----------
8789

@@ -125,6 +127,28 @@ Here is an example with a ping sent every 60 seconds, expecting a pong within 10
125127
pong_timeout=10,
126128
)
127129

130+
Underlying websockets protocol
131+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
132+
133+
In addition to the keep-alives described above for the apollo and graphql-ws protocols,
134+
there are also `ping frames`_ sent by the underlying websocket connection itself for both of them.
135+
136+
These pings are enabled by default (every 20 seconds) and could be modified or disabled
137+
by passing extra arguments to the :code:`connect` call of the websockets client using the
138+
:code:`connect_args` argument of the transport.
139+
140+
.. code-block:: python
141+
142+
# Disabling websocket protocol level pings
143+
transport = WebsocketsTransport(
144+
url='wss://SERVER_URL:SERVER_PORT/graphql',
145+
connect_args={"ping_interval": None},
146+
)
147+
148+
See the `websockets keepalive documentation`_ for details.
149+
128150
.. _version 5.6.1: https://github.com/enisdenjo/graphql-ws/releases/tag/v5.6.1
129151
.. _Apollo websockets transport protocol: https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md
130152
.. _GraphQL-ws websockets transport protocol: https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md
153+
.. _ping frames: https://www.rfc-editor.org/rfc/rfc6455.html#section-5.5.2
154+
.. _websockets keepalive documentation: https://websockets.readthedocs.io/en/stable/topics/timeouts.html#keepalive-in-websockets

gql/transport/websockets.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,19 @@ def __init__(
6464
a sign of liveness from the server.
6565
:param ping_interval: Delay in seconds between pings sent by the client to
6666
the backend for the graphql-ws protocol. None (by default) means that
67-
we don't send pings.
67+
we don't send pings. Note: there are also pings sent by the underlying
68+
websockets protocol. See the
69+
:ref:`keepalive documentation <websockets_transport_keepalives>`
70+
for more information about this.
6871
:param pong_timeout: Delay in seconds to receive a pong from the backend
6972
after we sent a ping (only for the graphql-ws protocol).
7073
By default equal to half of the ping_interval.
7174
:param answer_pings: Whether the client answers the pings from the backend
7275
(for the graphql-ws protocol).
7376
By default: True
74-
:param connect_args: Other parameters forwarded to websockets.connect
77+
:param connect_args: Other parameters forwarded to
78+
`websockets.connect <https://websockets.readthedocs.io/en/stable/reference/\
79+
client.html#opening-a-connection>`_
7580
:param subprotocols: list of subprotocols sent to the
7681
backend in the 'subprotocols' http header.
7782
By default: both apollo and graphql-ws subprotocols.

0 commit comments

Comments
 (0)