|
| 1 | +--- |
| 2 | +title: WebSocket protocol and Application Gateway for Containers |
| 3 | +description: Learn how the WebSocket protocol is used with Application Gateway for Containers. |
| 4 | +services: application gateway |
| 5 | +author: greg-lindsay |
| 6 | +ms.service: azure-appgw-for-containers |
| 7 | +ms.topic: conceptual |
| 8 | +ms.date: 1/13/2025 |
| 9 | +ms.author: greglin |
| 10 | +--- |
| 11 | + |
| 12 | +# WebSocket protocol and Application Gateway for Containers |
| 13 | + |
| 14 | +WebSockets, established in [RFC6455](https://datatracker.ietf.org/doc/html/rfc6455), enable two-way communication between a client and server. Unlike a traditional HTTP or HTTPS request that, WebSockets allow for the browser to establish a connection and receive continuous data from a server, without having to constantly pull the remote server or need to establish multiple connections in both directions (client to server and server to client). |
| 15 | + |
| 16 | +## WebSocket benefits |
| 17 | + |
| 18 | +The WebSocket protocol has several benefits over traditional HTTP requests, including: |
| 19 | + |
| 20 | +- Browser compatibility: Nearly all modern web browsers support WebSockets. |
| 21 | +- Real-time data: WebSockets enable real-time data transfer between client and server. |
| 22 | +- Efficiency: WebSockets eliminate the need to continuously poll servers to check for updates. |
| 23 | +- Security: WebSockets can be encrypted using TLS and use standard HTTP ports, such as 80 and 443. |
| 24 | +- Flexibility: WebSockets can be used for a variety of applications, including chat, gaming, and financial trading platforms. |
| 25 | + |
| 26 | +## How the WebSocket protocol works |
| 27 | + |
| 28 | +To establish a WebSocket connection, a specific HTTP-based handshake is exchanged between the client and the server. If successful, the application-layer protocol is "upgraded" from HTTP to WebSockets, using the previously established TCP connection. Once this occurs, the protocol is changed to WebSockets and traffic no longer flows over HTTP. Data is sent or received using the WebSocket protocol by both endpoints until the WebSocket connection is closed. |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +> [!NOTE] |
| 33 | +> After a connection is upgraded to WebSocket, as an intermediary/terminating proxy, Application Gateway for Containers will send the data received from the frontend to the backend and vice-versa, without any inspection or manipulation capability. |
| 34 | +> Therefore, any manipulations like Header Rewrites, URL Rewrites, or Overriding Hostname will not apply after establishing a WebSocket connection. |
| 35 | +
|
| 36 | +WebSocket connections may be either in plain-text or encrypted over TLS. When a connection is established over plain-text, the connection is established in the format of ws://\<fqdn\>/path. When a connection is established over TLS, the connection is established in the format of wss://\<fqdn\>/path. |
| 37 | + |
| 38 | +### Health probes |
| 39 | + |
| 40 | +No configuration is required to leverage a WebSocket request in Application Gateway for Containers, however you must ensure you properly configure health probes to ensure the backend is reflected as healthy. |
| 41 | + |
| 42 | +By default, Application Gateway for Containers attempts to initiate an HTTP handshake to the backend port running the WebSocket service. In many cases, this erroneously labels the backend as unhealthy, so a HealthCheckPolicy should be defined to ensure the health probe considers use of a TCP probe. |
| 43 | + |
| 44 | +Here's an example of a HealthCheckPolicy for a WebSocket backend. |
| 45 | + |
| 46 | +```bash |
| 47 | +kubectl apply -f - <<EOF |
| 48 | +apiVersion: alb.networking.azure.io/v1 |
| 49 | +kind: HealthCheckPolicy |
| 50 | +metadata: |
| 51 | + name: websockets-health-check-policy |
| 52 | + namespace: test-infra |
| 53 | +spec: |
| 54 | + targetRef: |
| 55 | + group: "" |
| 56 | + kind: Service |
| 57 | + name: websockets-backend |
| 58 | + namespace: test-infra |
| 59 | + default: |
| 60 | + interval: 5s |
| 61 | + timeout: 3s |
| 62 | + healthyThreshold: 1 |
| 63 | + unhealthyThreshold: 1 |
| 64 | + http: |
| 65 | + path: /health |
| 66 | +EOF |
| 67 | +``` |
| 68 | + |
| 69 | +>[!Note] |
| 70 | +>WebSockets are only supported when using Gateway API for Application Gateway for Containers. |
| 71 | +
|
| 72 | +### Metrics & Monitoring |
| 73 | + |
| 74 | +WebSockets Metrics: |
| 75 | + |
| 76 | +- New Connection Upgrades: This metric incrments on new WebSocket connections created (or upgraded) from regular HTTP requests. |
| 77 | +- Current Connection Upgrades: This metrics reflects active upgraded connections. |
| 78 | + |
| 79 | +>[!Note] |
| 80 | +>Connection upgrade metrics also include other connection upgrades, such as HTTP 1.1 to HTTP 2. |
| 81 | +
|
| 82 | +Diagnostic Logs: |
| 83 | + |
| 84 | +WebSocket connections operate using a distinct protocol. Upon initiating the connection, the browser receives an HTTP 101 status code, indicating the switch from HTTP to WebSocket and will be reflected in the access log. |
| 85 | + |
| 86 | +The details of the WebSocket connection are recorded only when the connection closes. This allows the duration of each connection to be accurately measured. |
| 87 | + |
| 88 | +## Next steps |
| 89 | + |
| 90 | +[Learn more](how-to-websockets-gateway-api.md) about WebSockets and Gateway API |
0 commit comments