2020-01-14 15:57:45 +03:00
.. meta ::
:description: Manage subscriptions with Hasura
:keywords: hasura, docs, subscription
2020-03-11 22:42:36 +03:00
.. _subscriptions:
2018-09-11 14:11:24 +03:00
Subscriptions
=============
2018-12-03 15:12:24 +03:00
.. contents :: Table of contents
:backlinks: none
:depth: 1
:local:
2019-12-15 16:02:10 +03:00
Introduction
------------
A GraphQL subscription is essentially a query where the client receives an update whenever the value of any field
changes upstream.
Subscriptions are supported for all kinds of queries. All the concepts of
2020-03-11 22:42:36 +03:00
:ref: `queries <queries>` hold true for subscriptions as well.
2018-09-11 14:11:24 +03:00
2019-12-15 16:02:10 +03:00
Execution
---------
The Hasura GraphQL engine subscriptions are actually **live queries** , i.e. a subscription will return the
latest result of the query being made and not necessarily all the individual events leading up to the result.
By default updates are delivered to clients every **1 sec** . This interval can be configured via the
`` HASURA_GRAPHQL_LIVE_QUERIES_MULTIPLEXED_REFETCH_INTERVAL `` env var or the
`` --live-queries-multiplexed-refetch-interval `` flag. See the
2020-03-11 22:42:36 +03:00
:ref: `server flag reference <server_flag_reference>` for info on setting the flag/env var.
2019-12-15 16:02:10 +03:00
2020-07-09 11:42:33 +03:00
You can read more about the implementation of subscriptions in the `architecture doc <https://github.com/hasura/graphql-engine/blob/master/architecture/live-queries.md> `__ .
2019-12-15 16:02:10 +03:00
2018-12-03 15:12:24 +03:00
Convert a query to a subscription
---------------------------------
2018-09-11 14:11:24 +03:00
You can turn any query into a subscription by simply replacing `` query `` with `` subscription `` as the operation type.
.. admonition :: Caveat
2020-07-09 11:42:33 +03:00
Hasura follows the `GraphQL spec <https://graphql.github.io/graphql-spec/June2018/#sec-Single-root-field> `__ which
2018-12-24 10:01:17 +03:00
allows for only one root field in a subscription.
2018-09-11 14:11:24 +03:00
2018-12-03 15:12:24 +03:00
Use cases
---------
2018-09-11 14:11:24 +03:00
- :ref: `subscribe_field`
- :ref: `subscribe_table`
- :ref: `subscribe_derived`
2018-12-03 15:12:24 +03:00
Communication protocol
----------------------
2018-09-11 14:11:24 +03:00
2019-03-06 11:58:04 +03:00
Hasura GraphQL engine uses the `GraphQL over WebSocket Protocol
2020-07-09 11:42:33 +03:00
<https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md> `__ by the
`apollographql/subscriptions-transport-ws <https://github.com/apollographql/subscriptions-transport-ws> `__ library
2018-09-11 14:11:24 +03:00
for sending and receiving events.
2019-03-06 11:58:04 +03:00
.. admonition :: Setting headers for subscriptions with Apollo client
2019-04-17 16:37:42 +03:00
If you are using Apollo Client, headers can be passed to a subscription by setting `` connectionParams `` while
2019-10-10 18:58:24 +03:00
`creating the wsLink <https://www.apollographql.com/docs/react/data/subscriptions/#client-setup> `_ :
2019-03-06 11:58:04 +03:00
.. code-block :: js
2019-04-17 16:37:42 +03:00
:emphasize-lines: 6-8
2019-03-06 11:58:04 +03:00
2019-04-17 16:37:42 +03:00
// Create a WebSocket link:
const wsLink = new WebSocketLink({
uri: `<graphql-endpoint>` ,
options: {
reconnect: true,
connectionParams: {
headers: {headers-object}
}
2019-03-06 11:58:04 +03:00
}
2019-04-17 16:37:42 +03:00
});
2020-07-09 11:42:33 +03:00
See `this <https://www.apollographql.com/docs/react/data/subscriptions/#authentication-over-websocket> `__ for more
2019-12-15 16:02:10 +03:00
info on using `` connectionParams `` .
2019-04-17 16:37:42 +03:00
2019-03-06 11:58:04 +03:00
Cookies and WebSockets
----------------------
2019-09-11 10:17:14 +03:00
The Hasura GraphQL engine will read cookies sent by the browser when initiating a
WebSocket connection. The browser will send the cookie only if it is a secure cookie
2019-03-04 10:46:53 +03:00
(`` secure `` flag in the cookie) and if the cookie has a `` HttpOnly `` flag.
Hasura will read this cookie and use it as headers when resolving authorization
(i.e. when resolving the auth webhook).
2019-03-06 11:58:04 +03:00
Cookies, WebSockets and CORS
2019-03-04 10:46:53 +03:00
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2019-09-11 10:17:14 +03:00
As browsers don't enforce Same Origin Policy (SOP) for websockets, the Hasura server
2019-03-04 10:46:53 +03:00
enforces the CORS rules when accepting the websocket connection.
It uses the provided CORS configuration (as per :ref: `configure-cors` ).
2019-09-11 10:17:14 +03:00
1. When it is `` * `` , the cookie is read and the CORS check is not enforced.
2019-03-04 10:46:53 +03:00
2019-09-11 10:17:14 +03:00
2. When there are explicit domains, the cookie will only be read if the request originates from one of
the listed domains.
2019-03-04 10:46:53 +03:00
2019-09-11 10:17:14 +03:00
3. If CORS is disabled, the default behaviour is that the cookie won't be read
2019-03-04 10:46:53 +03:00
(because of potential security issues). To override the behaviour, you can
2019-09-11 10:17:14 +03:00
use the flag `` --ws-read-cookie `` or the environment variable
2019-03-04 10:46:53 +03:00
`` HASURA_GRAPHQL_WS_READ_COOKIE `` . See
2020-03-11 22:42:36 +03:00
:ref: `server_flag_reference` for the setting.
2019-03-04 10:46:53 +03:00
2018-09-11 14:11:24 +03:00
.. toctree ::
:maxdepth: 1
:hidden:
2018-12-24 10:01:17 +03:00
Sample use cases <use-cases>