[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-29。"],[[["\u003cp\u003eThis content covers the use of WebSockets with both Apigee and Apigee hybrid platforms, explaining their native support for both HTTP and WS protocols within environment groups.\u003c/p\u003e\n"],["\u003cp\u003eApigee supports WebSockets through an upgrade request from HTTP to WS initiated by the client, resulting in a \u003ccode\u003e101 Switching Protocols\u003c/code\u003e response, and the subsequent establishment of a bidirectional connection.\u003c/p\u003e\n"],["\u003cp\u003ePolicies within a web socket proxy will function until the initial handshake is completed, and after the handshake no further policies will be executed, but existing OAuth tokens are still validated.\u003c/p\u003e\n"],["\u003cp\u003eWebSockets connections in Apigee can be terminated if requests lack valid API keys or OAuth tokens, if the provided tokens are invalid or expired, or due to connection timeouts.\u003c/p\u003e\n"],["\u003cp\u003eThe Apigee Debug tool will display each WebSockets connection as a single request with a \u003ccode\u003e101 Status\u003c/code\u003e, while the analytics dashboard will count each WebSockets session but will not display the traffic sent back and forth during the connection.\u003c/p\u003e\n"]]],[],null,["# Using WebSockets\n\n*This page\napplies to **Apigee** and **Apigee hybrid**.*\n\n\n*View [Apigee Edge](https://docs.apigee.com/api-platform/get-started/what-apigee-edge) documentation.*\n\n\nThis topic discusses how to use [WebSockets](https://en.wikipedia.org/wiki/WebSocket)\nwith Apigee and Apigee hybrid.\n\nIntroduction\n------------\n\nThere are many situations where web interactions need to happen in real time, such as gaming,\ncommunications, financial transactions, and other high-throughput activities.\n\nWebSocket is a\nprotocol that provides a full-duplex communications channel between a web client and web\nserver over a single TCP connection. The WebSocket protocol uses the HTTP protocol to\nestablish the connection between the client and server. Once established, the client and\nserver then use the WebSocket protocol to send and receive data.\n\nThe [WebSockets spec](https://html.spec.whatwg.org/multipage/web-sockets.html)\nand protocol is maintained by the W3C.\n\nSupport for WebSockets in Apigee\n--------------------------------\n\n\nIn Apigee and Apigee hybrid, environment groups provide routing to environments and define the\nhostnames on which API proxies are exposed. Environment groups support both the HTTP and WS\nprotocols natively. You do not have to create a special environment group or any special\nconfiguration to use WebSockets. Rather, it is up\nto the client to request\na protocol upgrade from HTTP to WS by including the `Upgrade` request header. An upgrade request made\nto an API proxy endpoint returns a `101 Switching Protocols` response. Further requests\nand responses are made bidirectionally on the open WebSockets connection, until it is closed.\n\n### Policy support\n\n\nAll policies will work in a web socket proxy until the handshake is completed\n(i.e. until the `HTTP 101` response is returned to the client),\nafter that no policy will execute, but OAuth tokens validated before the\nhandshake are still honored and the connection dropped if the token expires or\nis revoked.\n\n### Revoking the connection\n\n\nThe WebSockets connection is closed when:\n\n- The proxy endpoint receives a request without an API key or OAuth token.\n- The proxy endpoint receives a request with an expired or invalid API key or OAuth token.\n- The WebSockets connection times out.\n\n### Using debug and analytics\n\n\nIn the Debug tool, for each WebSockets connection you'll see one request appear with a\n**101 Status**.\n\n\nIn the Apigee analytics dashboard, you will see traffic counted for each WebSockets\nsession. Back and forth traffic that occurs during the session is not shown in the\ndashboard.\n\nSample WebSockets application\n-----------------------------\n\nTo learn more, you can refer to a working\n[websocket-sample](https://github.com/srinandan/websocket-sample) application on GitHub.\nThe sample demonstrates how to call an API proxy over a WebSockets connection. The proxy\ncalls a simple backend target application that is deployed in your Kubernetes cluster."]]