Websocket Subprotocols
This section explains how you can configure Websocket subprotocols both for local development and using Cosmo Studio.
The available WebSocket subprotocols are:-
-
auto: This is the default subprotocol. If auto is selected, we negotiate with the subgraph for the subprotocol. The subprotocols graphql-ws and graphql-transport-ws are used for the negotiation. If the subgraph doesn’t return a subprotocol, it falls back to graphql-ws.
-
graphql-ws
-
graphql-transport-ws
How to set the WebSocket subprotocol
- While creating a subgraph, the WebSocket subprotocol can be set using the create subgraph command.
- If the subgraph is already created, it can be updated using the update subgraph command.
Configure Subscriptions for local development
When composing a Graph locally, you can supply the subprotocol using the following configuration.
WebSocket Authentication
Cosmo Router supports WebSocket authentication and offers two configurable options for handling it
-
via Headers
-
via Initial Payload
Default WebSocket Authentication behaviour
By default, the Router expects the JWT (JSON Web Token) to be included in the request headers. This token is then used to authenticate the WebSocket connection. If the Router makes subsequent Subgraph requests via HTTP(S), the header can be forwarded to the Subgraphs.
WebSocket Authentication via initial payload
Some clients, e.g. browsers, cannot set Headers when initiating a WebSocket connection. As such, the default behaviour to authenticate via Authorization Header is not suitable.
For such cases, you can configure the Router to accept clients to Authenticate using the initial payload when initiating a WebSocket connection.
When enabled, a client can set up a WebSocket connection and then send a JWT as part of the initial WebSocket message using the initial_payload
field.
In addition to allowing clients to authenticate using this method, you can also enable the export_token
option. This feature takes the token from the initial payload and exports it into the Header of the Upgrade request. This allows the Router to treat every Subscription from this client as if the client sent the token as a header. Consequently, if header forwarding rules are enabled, the exported token will be forwarded accordingly.