Import a WebSocket API
With API Management’s WebSocket API solution, you can now manage, protect, observe, and expose both WebSocket and REST APIs with API Management and provide a central hub for discovering and consuming all APIs. API publishers can quickly add a WebSocket API in API Management via:
- A simple gesture in the Azure portal, and
- The Management API and Azure Resource Manager.
You can secure WebSocket APIs by applying existing access control policies, like JWT validation. You can also test WebSocket APIs using the API test consoles in both Azure portal and developer portal. Building on existing observability capabilities, API Management provides metrics and logs for monitoring and troubleshooting WebSocket APIs.
In this article, you will:
- Understand Websocket passthrough flow.
- Add a WebSocket API to your API Management instance.
- Test your WebSocket API.
- View the metrics and logs for your WebSocket API.
- Learn the limitations of WebSocket API.
- An existing API Management instance. Create one if you haven't already.
- A WebSocket API.
API Management supports WebSocket passthrough.
During the WebSocket passthrough the client application establishes a WebSocket connection with the API Management Gateway, which then establishes a connection with the corresponding backend services. API Management then proxies WebSocket client-server messages.
- The client application sends a WebSocket handshake request to APIM gateway, invoking onHandshake operation.
- APIM gateway sends WebSocket handshake request to the corresponding backend service.
- The backend service upgrades a connection to WebSocket.
- APIM gateway upgrades the corresponding connection to WebSocket.
- Once the connection pair is established, APIM will broker messages back and forth between the client application and backend service.
- The client application sends message to APIM gateway.
- APIM gateway forwards the message to the backend service.
- The backend service sends a message to APIM gateway.
- APIM gateway forwards the message to the client application.
- When either side disconnects, APIM terminates the corresponding connection.
The client-side and backend-side connections consist of one-to-one mapping.
Per the WebSocket protocol, when a client application tries to establish a WebSocket connection with a backend service, it will first send an opening handshake request. Each WebSocket API in API Management has an onHandshake operation. onHandshake is an immutable, unremovable, automatically created system operation. The onHandshake operation enables API publishers to intercept these handshake requests and apply API Management policies to them.
Add a WebSocket API
Navigate to your API Management instance.
From the side navigation menu, under the APIs section, select APIs.
Under Define a new API, select the WebSocket icon.
In the dialog box, select Full and complete the required form fields.
Field Description Display name The name by which your WebSocket API will be displayed. Name Raw name of the WebSocket API. Automatically populates as you type the display name. WebSocket URL The base URL with your websocket name. For example: ws://example.com/your-socket-name URL scheme Accept the default API URL suffix Add a URL suffix to identify this specific API in this API Management instance. It has to be unique in this APIM instance. Products Associate your WebSocket API with a product to publish it. Gateways Associate your WebSocket API with existing gateways.
Test your WebSocket API
Navigate to your WebSocket API.
Within your WebSocket API, select the onHandshake operation.
Select the Test tab to access the Test console.
Optionally, provide query string parameters required for the WebSocket handshake.
View connection status in Output.
Enter value in Payload.
View received messages in Output.
Repeat preceding steps to test different payloads.
When testing is complete, select Disconnect.
View metrics and logs
Use standard API Management and Azure Monitor features to monitor WebSocket APIs:
- View API metrics in Azure Monitor
- Optionally enable diagnostic settings to collect and view API Management gateway logs, which include WebSocket API operations
For example, the following screenshot shows recent WebSocket API responses with code
101 from the ApiManagementGatewayLogs table. These results indicate the successful switch of the requests from TCP to the WebSocket protocol.
Below are the current restrictions of WebSocket support in API Management:
- WebSocket APIs are not supported yet in the Consumption tier.
- WebSocket APIs are not supported yet in the self-hosted gateway.
- Azure CLI, PowerShell, and SDK currently do not support management operations of WebSocket APIs.
- 200 active connections limit per unit.
- Websockets APIs support the following valid buffer types for messages: Close, BinaryFragment, BinaryMessage, UTF8Fragment, and UTF8Message.
The following policies are not supported by and cannot be applied to the onHandshake operation:
- Mock response
- Get from cache
- Store to cache
- Allow cross-domain calls
- Set request method
- Set body
- Convert XML to JSON
- Convert JSON to XML
- Transform XML using XSLT
- Validate content
- Validate parameters
- Validate headers
- Validate status code
If you applied the policies at higher scopes (i.e., global or product) and they were inherited by a WebSocket API through the policy, they will be skipped at run time.
- API import limitations
- Import an OpenAPI specification
- Import a SOAP API
- Import a SOAP API and convert to REST
- Import an App Service API
- Import a Container App API
- Import a WebSocket API
- Import a GraphQL API (preview)
- Import an Azure Function App
- Import an Azure Logic App
- Import a Service Fabric service
- Import SAP OData metadata
- Edit an API
Submit and view feedback for