# WebSocket and GraphQL

> Inspect WebSocket frames and GraphQL operations in Cloak Proxy. Filter messages by direction, search payloads, and read formatted queries and variables.

Source: https://docs.subcloak.com/cloak-proxy/inspect/websocket-and-graphql/

Two kinds of traffic get their own views in the [inspector](https://docs.subcloak.com/cloak-proxy/inspect/inspector/). WebSocket connections show every frame sent and received in a **Messages** tab, live as they arrive. GraphQL requests sent over HTTP get a **GraphQL** tab with the query formatted and the variables in a JSON tree, and the operation name shows in the traffic list. GraphQL subscriptions, over a WebSocket or SSE, get their own **GraphQL** tab on the response, described in [Streaming responses](https://docs.subcloak.com/cloak-proxy/inspect/streams/#graphql-subscriptions).

## WebSocket messages

Cloak Proxy recognizes a WebSocket when the request asks to upgrade the connection (an `Upgrade: websocket` header). It relays the frames between the app and the server and records each one.

1. Make sure the connection is decrypted. For `wss://` connections, turn on [SSL Proxying](https://docs.subcloak.com/cloak-proxy/capture/ssl-proxying/) for the host or the app. Plain `ws://` connections need nothing extra. (added in Cloak Proxy 0.5.1) Servers with a self-signed certificate need to be in [Servers without certificate checks](https://docs.subcloak.com/cloak-proxy/capture/network-settings/#skip-certificate-checks-for-some-servers), and a server that asks for a client certificate gets the one in [Client certificates](https://docs.subcloak.com/cloak-proxy/capture/certificate/#present-client-certificates-mutual-tls), as for HTTPS requests. When the connection to the server fails, the app gets **502 Bad Gateway** and the socket in the list says why.
2. Click the **WebSocket** tab in the [filter bar](https://docs.subcloak.com/cloak-proxy/inspect/filtering/) to find WebSocket connections quickly.
3. Select the connection in the traffic list.

   The response pane shows a **Messages** tab, with the frame count, in place of **Body**. The other response tabs are **Header**, **Set-Cookie** (when the handshake set cookies) and **Raw**.

4. Click a message to see its contents in the lower half of the pane.

*[Screenshot: The Messages tab of a WebSocket connection: All, Sent and Received buttons, a Hide ping/pong checkbox, a search field, the count 3 of 4 messages, and a table of a sent subscribe message, a received order message and a sent binary frame. Frames on a WebSocket connection. Sent frames have a blue up arrow, received frames a green down arrow.]*

### Messages table

| Column | Shows |
|---|---|
| Direction | A blue up arrow for frames the app sent, a green down arrow for frames it received |
| **Time** | When the frame passed through, to the millisecond |
| **Type** | `text`, `binary`, `ping`, `pong`, `close` or `continuation` |
| **Size** | The payload size. A message over 1 MB shows **truncated** with its full size. (added in Cloak Proxy 0.5.1) |
| **Data** | Text payloads on one line. Other frames show their type, such as `<binary data>` or `<ping data>`. |

### Filter and search messages

| Control | What it does |
|---|---|
| **All** / **Sent** / **Received** | Shows every frame, or only one direction |
| **Hide ping/pong** | Hides keep-alive frames. On by default. |
| **Search messages** | Shows text frames that contain your text, ignoring case. Binary frames are skipped while searching. |
| Count | **N messages**, or **N of M messages** while filtering |

### Read a message

Select a frame and its payload opens in the [body viewer](https://docs.subcloak.com/cloak-proxy/inspect/body-viewers/) below the table. JSON text opens as a tree you can filter and query with JSONPath or jq. Other text opens as **Raw**, and binary payloads that aren't text open in **Hex**. Until you select one, the lower half says **Select a message to see its contents**.

While the connection is open but quiet, the tab shows **Connected. Waiting for messages…**. If it closed without any frames, it shows **No messages were exchanged.**

> **What Cloak Proxy changes:**
> To keep frames readable, Cloak Proxy doesn't pass on the app's offer to compress WebSocket messages (the `permessage-deflate` extension), so the app and server talk uncompressed. Each connection keeps its most recent 5,000 frames. The connection's **Duration** in the traffic list covers the whole time it was open.
>
> Messages of any size pass through whole. (added in Cloak Proxy 0.5.1) Cloak Proxy reads frames as they stream and keeps the first 1 MB of each message for the inspector (text is cut between characters); a longer one shows **truncated** with its full size. Up to 0.5.0, a message over 64 MB closed the connection (1009).

*[Screenshot: The Messages tab of a WebSocket connection: a subscribe message sent, an order message received, a 4-byte binary frame sent, and a 1 MB text message received marked truncated (1.3 MB) after its data. A 1.3 MB message, kept up to 1 MB and marked truncated. The app and the server got all of it.]*

To send your own WebSocket messages, use the [WebSocket client](https://docs.subcloak.com/cloak-proxy/api-client/protocols/websocket/) in the API client.

## GraphQL operations

Most GraphQL APIs send every operation to the same URL, often `/graphql`, which makes them hard to tell apart in a list. Cloak Proxy reads the request body and labels each one.

A request counts as GraphQL when its body (up to 2 MB) is a JSON object with a GraphQL `query` document or a persisted-query `operationName`, a batch (a JSON array of those), an `application/graphql` document, or when a `GET` request has a `query` parameter holding a GraphQL document. A search API's `{"query": "red shoes"}` doesn't count. See [GraphQL operations](https://docs.subcloak.com/cloak-proxy/tools/conditions-and-graphql/#graphql-operations) for the details.

### In the traffic list

The **Path** column starts with a pink badge naming the operation, such as **query GetUser** or **mutation UpdateCart**. The name comes from the body's `operationName`, or from the query document itself (`query GetUser(…)`). Anonymous operations show only the type: **query**, **mutation** or **subscription**.

Click the **GraphQL** tab in the [filter bar](https://docs.subcloak.com/cloak-proxy/inspect/filtering/) to show only GraphQL requests. GraphQL requests also count as **JSON**.

> **Tip:**
> To find one operation, click it in the sidebar's **GraphQL** group, or filter with **Match in › GraphQL** and **Contains** set to its name, for example `GetUser`.

(added in Cloak Proxy 0.4.0) A `200` whose body has a GraphQL `errors` array shows a red **200 ✕** status and a red banner above the response, and the sidebar's **GraphQL** group lists every operation. To find one operation, click it there, or filter with **Match in › GraphQL**. See [GraphQL errors in a 200 response](https://docs.subcloak.com/cloak-proxy/tools/conditions-and-graphql/#graphql-errors-in-a-200-response).

### The GraphQL tab

The request pane gets a **GraphQL** tab with two parts.

*[Screenshot: The GraphQL tab: the query GetUser with an id variable, formatted over several lines, above a Variables tree with id set to the string 42. The query, formatted, above its variables.]*

| Part | Shows |
|---|---|
| **Query** | The query document, re-indented with one field per line, as selectable text. Press <kbd>⌘</kbd> <kbd>F</kbd> inside it to search. |
| **Variables** | The `variables` object in a [JSON tree](https://docs.subcloak.com/cloak-proxy/inspect/body-viewers/#explore-json-in-the-tree), with filtering and JSONPath or jq queries. **No variables** when there are none. |

Drag the line between the two parts to resize them. For a batch, the tab shows the first operation; for a `GET` request, the query and variables from the URL. If the request can't be read as GraphQL, the tab says **The body isn't valid GraphQL JSON.**

The response is ordinary JSON, so it opens in the **Body** tab's tree view, where you can filter for `errors` or query `$.data`.

For subscriptions and responses that arrive in parts (`@defer`, `@stream`), see [GraphQL subscriptions](https://docs.subcloak.com/cloak-proxy/inspect/streams/#graphql-subscriptions). To write and send GraphQL queries yourself, use the [GraphQL client](https://docs.subcloak.com/cloak-proxy/api-client/protocols/graphql/).

## Related

  - [Body viewers](https://docs.subcloak.com/cloak-proxy/inspect/body-viewers/): The JSON tree, JSONPath and jq used by both views.
  - [SSL Proxying](https://docs.subcloak.com/cloak-proxy/capture/ssl-proxying/): Decrypt wss:// and HTTPS GraphQL endpoints.
  - [WebSocket client](https://docs.subcloak.com/cloak-proxy/api-client/protocols/websocket/): Connect and send messages from the API client.
  - [GraphQL client](https://docs.subcloak.com/cloak-proxy/api-client/protocols/graphql/): Write, run and save GraphQL operations.
  - [Streaming responses](https://docs.subcloak.com/cloak-proxy/inspect/streams/): SSE, JSON lines and GraphQL subscriptions, event by event.
  - [Conditions and GraphQL operations](https://docs.subcloak.com/cloak-proxy/tools/conditions-and-graphql/): Rules for one GraphQL operation, and GraphQL errors in 200 responses.
