3
3
WebsocketsTransport
4
4
===================
5
5
6
- The websockets transport implements the `Apollo websockets transport protocol `_.
6
+ The websockets transport supports both:
7
+
8
+ - the `Apollo websockets transport protocol `_.
9
+ - the `GraphQL-ws websockets transport protocol `_
10
+
11
+ It will detect the backend supported protocol from the response http headers returned.
7
12
8
13
This transport allows to do multiple queries, mutations and subscriptions on the same websocket connection.
9
14
15
+ Reference: :py:class: `gql.transport.websockets.WebsocketsTransport `
16
+
10
17
.. literalinclude :: ../code_examples/websockets_async.py
11
18
12
19
Websockets SSL
13
20
--------------
14
21
15
22
If you need to connect to an ssl encrypted endpoint:
16
23
17
- * use _wss_ instead of _ws_ in the url of the transport
24
+ * use :code: ` wss ` instead of :code: ` ws ` in the url of the transport
18
25
19
26
.. code-block :: python
20
27
21
- sample_transport = WebsocketsTransport(
28
+ transport = WebsocketsTransport(
22
29
url = ' wss://SERVER_URL:SERVER_PORT/graphql' ,
23
30
headers = {' Authorization' : ' token' }
24
31
)
@@ -34,7 +41,7 @@ If you have a self-signed ssl certificate, you need to provide an ssl_context wi
34
41
localhost_pem = pathlib.Path(__file__ ).with_name(" YOUR_SERVER_PUBLIC_CERTIFICATE.pem" )
35
42
ssl_context.load_verify_locations(localhost_pem)
36
43
37
- sample_transport = WebsocketsTransport(
44
+ transport = WebsocketsTransport(
38
45
url = ' wss://SERVER_URL:SERVER_PORT/graphql' ,
39
46
ssl = ssl_context
40
47
)
@@ -54,7 +61,7 @@ There are two ways to send authentication tokens with websockets depending on th
54
61
55
62
.. code-block :: python
56
63
57
- sample_transport = WebsocketsTransport(
64
+ transport = WebsocketsTransport(
58
65
url = ' wss://SERVER_URL:SERVER_PORT/graphql' ,
59
66
headers = {' Authorization' : ' token' }
60
67
)
@@ -63,9 +70,53 @@ There are two ways to send authentication tokens with websockets depending on th
63
70
64
71
.. code-block :: python
65
72
66
- sample_transport = WebsocketsTransport(
73
+ transport = WebsocketsTransport(
67
74
url = ' wss://SERVER_URL:SERVER_PORT/graphql' ,
68
75
init_payload = {' Authorization' : ' token' }
69
76
)
70
77
78
+ Keep-Alives
79
+ -----------
80
+
81
+ Apollo protocol
82
+ ^^^^^^^^^^^^^^^
83
+
84
+ With the Apollo protocol, the backend can optionally send unidirectional keep-alive ("ka") messages
85
+ (only from the server to the client).
86
+
87
+ It is possible to configure the transport to close if we don't receive a "ka" message
88
+ within a specified time using the :code: `keep_alive_timeout ` parameter.
89
+
90
+ Here is an example with 60 seconds::
91
+
92
+ transport = WebsocketsTransport(
93
+ url='wss://SERVER_URL:SERVER_PORT/graphql',
94
+ keep_alive_timeout=60,
95
+ )
96
+
97
+ One disadvantage of the Apollo protocol is that because the keep-alives are only sent from the server
98
+ to the client, it can be difficult to detect the loss of a connection quickly from the server side.
99
+
100
+ GraphQL-ws protocol
101
+ ^^^^^^^^^^^^^^^^^^^
102
+
103
+ With the GraphQL-ws protocol, it is possible to send bidirectional ping/pong messages.
104
+ Pings can be sent either from the client or the server and the other party should answer with a pong.
105
+
106
+ As with the Apollo protocol, it is possible to configure the transport to close if we don't
107
+ receive any message from the backend within the specified time using the :code: `keep_alive_timeout ` parameter.
108
+
109
+ But there is also the possibility for the client to send pings at a regular interval and verify
110
+ that the backend sends a pong within a specified delay.
111
+ This can be done using the :code: `ping_interval ` and :code: `pong_timeout ` parameters.
112
+
113
+ Here is an example with a ping sent every 60 seconds, expecting a pong within 10 seconds::
114
+
115
+ transport = WebsocketsTransport(
116
+ url='wss://SERVER_URL:SERVER_PORT/graphql',
117
+ ping_interval=60,
118
+ pong_timeout=10,
119
+ )
120
+
71
121
.. _Apollo websockets transport protocol : https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md
122
+ .. _GraphQL-ws websockets transport protocol : https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md
0 commit comments