Import a WebSocket API in API Management

Published Jan 07 2022 12:44 AM 2,206 Views
Microsoft

API Management is a proxy which help to forward the request from client side to destination API service. It has the ability to modify the request or process based on the inputs from the client side before it reaches the destination.

On the other hand, WebSocket APIs are used to build real-time applications such as stock market reports, live sports scores, real-time social feeds.

With native support for WebSocket APIs in Azure API Management, you can manage, protect, observe, and expose your WebSocket APIs, alongside REST and SOAP APIs.

With this capability, you can now:

  • Manage both WebSocket and REST APIs with API Management.
  • Quickly add a WebSocket API in API Management via a simple gesture in the Azure portal, or via the management API and Azure Resource Manager.
  • Secure WebSocket APIs by applying existing access control policies (e.g., JWT validation).
  • Test WebSocket APIs using the API test consoles in both Azure portal and developer portal.
  • Get metrics and logs for monitoring and troubleshooting WebSocket APIs.

Note: This is available in all API Management pricing tiers (except Consumption).

 

WebSocket Protocol – Key Benefits over HTTP​

  • In WebSocket’s, client and server can exchange messages independently. In HTTP based communications such as REST, client must initiate communication with server and “poll” the server for a response, which is inefficient.​
  • A single persistent connection that removes the overhead of expensive SSL handshake and re-establishing connections and reduces latency.

Understand how Web Socket works in APIM:

 

  • Client sends http(s) request to APIM to ask for establishing WebSocket connection
    • The http request must include the following headers
      • Connection: Upgrade
      • Upgrade: Websocket
      • Sec-WebSocket-Version: 13
    • Once APIM receives WebSocket connection request from Client, APIM then executes defined policies. The connection request is then forwarded to the backend.
    • Once Server agrees the connection request, it returned http response back to APIM with http status code 101. APIM then returns the response back to client.
    • Once the WebSocket connection is established, client can send message in WebSocket protocol to backend server through APIM.

      SherrySahni_1-1641539480945.png

       

Configure APIM with WebPubSub service:

 

Steps to create Azure Web PubSub :

 

 

SherrySahni_2-1641539480956.png

 

  • Please note that WebSocket endpoints are prefixed with WS:// (non-secure, port 80 by default) or WSS:// (secure, port 443 by default)
  • We can use the Client Access URL to do some quick connect test  using the built in tool from Web Sub.
    Client Access URLhttps://azure.github.io/azure-webpubsub/demos/clientpubsub.html

SherrySahni_3-1641539480964.png

 

Steps to Add Websocket API in APIM:

 

  • Navigate to the API Management service
  • Go to APIs => Add API => WebSocket
    SherrySahni_4-1641539480986.png
  • In the "Create a WebSocket API" dialog:
    • WebSocket URL is in a form of wss://<your_webpubsub_name>.webpubsub.azure.com/client/hubs/<your_hub_name>
      • Replace <your_webpubsub_name> with the name of your Web PubSub resource
      • Replace <your_hub_name> with the name of the hub you use. For example if your Web PubSub resource is demo, and the hub name is chatdemo, the WebSocket URL should be "wss://demo.webpubsub.azure.com/client/hubs/chatdemo"
    • Make sure to have the API URL suffix in the form of "client/hubs/<your_hub_name>" and replace "<your_hub_name>" with the name of your hub

SherrySahni_5-1641539480994.png

 

  •  Test the API
    • Use the Test tab, add "access_token" query parameter
      • Go to the Web PubSub resource, go to the Keys tab, and use the Client URL Generator to generate the Client URL which contains a temp value for access_token
      •  Make sure the Hub name is the same name as the one set in APIM
      • The "Client Access URL" will have a form of: wss://<your_svc_name>.webpubsub.azure.com/client/hubs/<your_hub_name>?access_token=<TokenToCopy>
        • Copy the value of the access_token to the query parameter of the APIM and click Connect.

SherrySahni_6-1641539481004.png

 

    

SherrySahni_7-1641539481018.png

 

Note:

  • When generating url (e.g. via serviceClient.GenerateClientAccessUri) make sure it contains both APIM key (in case it is used for auth) and the WPS access_token query string parameters:
    wss://your_service_name.azure-api.net/client/hubs/your_hub_name?subscription-key=<apim_key>&access_token=<wps_key>
  • If you don't want to (or it is not feasible) to generate the access_token that APIM would just normally pass the to WebPubSub service, you could allow anonymous access and configure private VNET to limit access to WebPubSub from APIM only.


Common Scenario when client failed to establish Web Socket connection to APIM and to backend -

Scenario 1 : Invalid websocket upgrade request

  • When we see the above error message, we will need to check the followings
    • Connection Request must have the following three headers. We can use fiddler to capture request and confirm request header
      • Connection: Upgrade
      • Upgrade: Websocket
      • Sec-WebSocket-Version: 13

SherrySahni_8-1641539481047.png

 

  • Request URL endpoint must exist APIM.
  • For example, my websocket URL endpoint in APIM is wss://demo-apim-oauth.azure-api.net/wsapiwebscoketdemo, then the request URL that client sends cannot be wss://demo-apim-oauth.azure-api.net/wsapiwebscoketdemo/{extraParam} 

 

Referencehttps://docs.microsoft.com/en-us/azure/api-management/websocket-api

Happy Learning! :)

 

2 Comments
%3CLINGO-SUB%20id%3D%22lingo-sub-3054085%22%20slang%3D%22en-US%22%3EImport%20a%20WebSocket%20API%20in%20API%20Management%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3054085%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EAPI%26nbsp%3BManagement%3C%2FSTRONG%3E%20is%20a%20proxy%20which%26nbsp%3Bhelp%26nbsp%3Bto%26nbsp%3Bforward%26nbsp%3Bthe%20request%20from%20client%20side%20to%20destination%20API%20service.%20It%26nbsp%3Bhas%20the%20ability%20to%26nbsp%3Bmodify%26nbsp%3Bthe%20request%20or%20process%20based%20on%20the%26nbsp%3Binputs%26nbsp%3Bfrom%20the%20client%20side%20before%20it%20reaches%20the%20destination.%3CBR%20%2F%3E%3CBR%20%2F%3EOn%20the%20other%20hand%3CSTRONG%3E%2C%20WebSocket%20APIs%3C%2FSTRONG%3E%20are%20used%20to%20build%26nbsp%3Breal-time%20applications%20such%20as%20stock%20market%20reports%2C%20live%20sports%20scores%2C%20real-time%20social%26nbsp%3Bfeeds.%3C%2FP%3E%0A%3CP%3EWith%20native%20support%20for%20WebSocket%20APIs%20in%20Azure%20API%20Management%2C%20you%20can%26nbsp%3Bmanage%2C%20protect%2C%20observe%2C%20and%20expose%20your%20WebSocket%20APIs%2C%20alongside%20REST%20and%20SOAP%20APIs.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EWith%20this%20capability%2C%20you%20can%20now%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EManage%20both%20WebSocket%20and%20REST%20APIs%20with%20API%20Management.%3C%2FLI%3E%0A%3CLI%3EQuickly%20add%20a%20WebSocket%20API%20in%20API%20Management%20via%20a%20simple%20gesture%20in%20the%20Azure%20portal%2C%20or%20via%20the%20management%20API%20and%20Azure%20Resource%20Manager.%3C%2FLI%3E%0A%3CLI%3ESecure%20WebSocket%20APIs%20by%20applying%20existing%20access%20control%20policies%20(e.g.%2C%20JWT%20validation).%3C%2FLI%3E%0A%3CLI%3ETest%20WebSocket%20APIs%20using%20the%20API%20test%20consoles%20in%20both%20Azure%20portal%20and%20developer%20portal.%3C%2FLI%3E%0A%3CLI%3EGet%20metrics%20and%20logs%20for%20monitoring%20and%20troubleshooting%20WebSocket%20APIs.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSTRONG%3ENote%3C%2FSTRONG%3E%3A%20This%20is%20available%20in%20all%20API%20Management%20pricing%20tiers%20(except%20Consumption).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EWebSocket%20Protocol%20%E2%80%93%20Key%20Benefits%20over%20HTTP%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EIn%20WebSocket%E2%80%99s%2C%20client%20and%20server%20can%20exchange%20messages%26nbsp%3Bindependently.%20In%20HTTP%20based%20communications%20such%20as%26nbsp%3BREST%2C%20client%20must%20initiate%20communication%20with%20server%20and%26nbsp%3B%E2%80%9Cpoll%E2%80%9D%20the%20server%20for%20a%20response%2C%20which%20is%20inefficient.%20%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3EA%20single%20persistent%20connection%20that%20removes%20the%20overhead%26nbsp%3Bof%20expensive%20SSL%20handshake%20and%20re-establishing%26nbsp%3Bconnections%20and%20reduces%20latency.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSTRONG%3EUnderstand%20how%20Web%20Socket%20works%20in%20APIM%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EClient%20sends%26nbsp%3B%3CSTRONG%3Ehttp(s)%3C%2FSTRONG%3E%26nbsp%3Brequest%20to%20APIM%20to%20ask%20for%20establishing%20WebSocket%20connection%3CUL%3E%0A%3CLI%3EThe%20http%20request%20must%20include%20the%20following%20headers%3CUL%3E%0A%3CLI%3EConnection%3A%20Upgrade%3C%2FLI%3E%0A%3CLI%3EUpgrade%3A%20Websocket%3C%2FLI%3E%0A%3CLI%3ESec-WebSocket-Version%3A%2013%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3EOnce%20APIM%20receives%20WebSocket%20connection%20request%20from%20Client%2C%20APIM%20then%20executes%20defined%20policies.%20The%20connection%20request%20is%20then%20forwarded%20to%20the%20backend.%3C%2FLI%3E%0A%3CLI%3EOnce%20Server%20agrees%20the%20connection%20request%2C%20it%20returned%20http%20response%20back%20to%20APIM%20with%20http%20status%20code%20101.%20APIM%20then%20returns%20the%20response%20back%20to%20client.%3C%2FLI%3E%0A%3CLI%3EOnce%20the%20WebSocket%20connection%20is%20established%2C%20client%20can%20send%20message%20in%20WebSocket%20protocol%20to%20backend%20server%20through%20APIM.%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22SherrySahni_1-1641539480945.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22SherrySahni_1-1641539480945.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F337913i25D34390875579BE%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22SherrySahni_1-1641539480945.png%22%20alt%3D%22SherrySahni_1-1641539480945.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId-334648849%22%20id%3D%22toc-hId-338230280%22%3EConfigure%20APIM%20with%20WebPubSub%20service%3A%3C%2FH2%3E%0A%3CUL%3E%0A%3CLI%3EAn%20existing%20API%20Management%20instance.%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fget-started-create-service-instance%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ECreate%20one%20if%20you%20haven't%20already%3C%2FA%3E.%3C%2FLI%3E%0A%3CLI%3EA%20WebSocket%20API.%20(in%20this%20demo%20we%20will%20be%20using%20Azure%20Web%20PubSub%20Service%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-in%2Fazure%2Fazure-web-pubsub%2Foverview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-in%2Fazure%2Fazure-web-pubsub%2Foverview%3C%2FA%3E)%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ESteps%20to%20create%20Azure%20Web%20PubSub%20%3A%3C%2FSTRONG%3E%3CSTRONG%3E%3CBR%20%2F%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ECreate%20an%20Azure%20Web%20PubSub%20service%20instance%20following%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-gb%2Fazure%2Fazure-web-pubsub%2Fquickstart-live-demo%23create-an-azure-web-pubsub-service-instance%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-gb%2Fazure%2Fazure-web-pubsub%2Fquickstart-live-demo%23create-an-azure-web-pubsub-service-instance%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3EOnce%20the%20Web%20PubSub%20service%20gets%20created%2C%20you%20would%20be%20observing%20the%20Client%20Access%20URL%20under%20the%20Key%20Blade%20as%20shown%20below%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22SherrySahni_2-1641539480956.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22SherrySahni_2-1641539480956.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F337915i8EEB006B07F8ED6B%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22SherrySahni_2-1641539480956.png%22%20alt%3D%22SherrySahni_2-1641539480956.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EPlease%20note%20that%20WebSocket%20endpoints%20are%20prefixed%20with%20WS%3A%2F%2F%20(non-secure%2C%26nbsp%3Bport%2080%20by%20default)%20or%20WSS%3A%2F%2F%20(secure%2C%20port%20443%20by%20default)%3C%2FLI%3E%0A%3CLI%3EWe%20can%20use%20the%20Client%20Access%20URL%20to%20do%20some%20quick%20connect%20test%26nbsp%3B%20using%20the%20built%20in%20tool%20from%20Web%20Sub.%3CBR%20%2F%3E%3CSTRONG%3EClient%20Access%20URL%3C%2FSTRONG%3E%20%3A%26nbsp%3B%20%3CA%20href%3D%22https%3A%2F%2Fazure.github.io%2Fazure-webpubsub%2Fdemos%2Fclientpubsub.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fazure.github.io%2Fazure-webpubsub%2Fdemos%2Fclientpubsub.html%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22SherrySahni_3-1641539480964.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22SherrySahni_3-1641539480964.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F337914i3C046BE9F893ABBF%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22SherrySahni_3-1641539480964.png%22%20alt%3D%22SherrySahni_3-1641539480964.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ESteps%20to%20Add%20Websocket%20API%20in%20APIM%3A%3CBR%20%2F%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ENavigate%20to%20the%20API%20Management%20service%3C%2FLI%3E%0A%3CLI%3EGo%20to%20APIs%20%3D%26gt%3B%20Add%20API%20%3D%26gt%3B%20WebSocket%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22SherrySahni_4-1641539480986.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22SherrySahni_4-1641539480986.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F337916iAB86A3898774863D%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22SherrySahni_4-1641539480986.png%22%20alt%3D%22SherrySahni_4-1641539480986.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3EIn%20the%20%22Create%20a%20WebSocket%20API%22%20dialog%3A%3CUL%3E%0A%3CLI%3EWebSocket%20URL%20is%20in%20a%20form%20of%20wss%3A%2F%2F%3CYOUR_WEBPUBSUB_NAME%3E.webpubsub.azure.com%2Fclient%2Fhubs%2F%3CYOUR_HUB_NAME%3E%3CUL%3E%0A%3CLI%3EReplace%20%3CYOUR_WEBPUBSUB_NAME%3E%20with%20the%20name%20of%20your%20Web%20PubSub%20resource%3C%2FYOUR_WEBPUBSUB_NAME%3E%3C%2FLI%3E%0A%3CLI%3EReplace%20%3CYOUR_HUB_NAME%3E%20with%20the%20name%20of%20the%20hub%20you%20use.%20For%20example%20if%20your%20Web%20PubSub%20resource%20is%20demo%2C%20and%20the%20hub%20name%20is%20chatdemo%2C%20the%20WebSocket%20URL%20should%20be%20%22wss%3A%2F%2Fdemo.webpubsub.azure.com%2Fclient%2Fhubs%2Fchatdemo%22%3C%2FYOUR_HUB_NAME%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FYOUR_HUB_NAME%3E%3C%2FYOUR_WEBPUBSUB_NAME%3E%3C%2FLI%3E%0A%3CLI%3EMake%20sure%20to%20have%20the%20API%20URL%20suffix%20in%20the%20form%20of%20%22client%2Fhubs%2F%3CYOUR_HUB_NAME%3E%22%20and%20replace%20%22%3CYOUR_HUB_NAME%3E%22%20with%20the%20name%20of%20your%20hub%3C%2FYOUR_HUB_NAME%3E%3C%2FYOUR_HUB_NAME%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22SherrySahni_5-1641539480994.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22SherrySahni_5-1641539480994.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F337917i528706F1141CF665%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22SherrySahni_5-1641539480994.png%22%20alt%3D%22SherrySahni_5-1641539480994.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%26nbsp%3BTest%20the%20API%3CUL%3E%0A%3CLI%3EUse%20the%20Test%20tab%2C%20add%20%22access_token%22%20query%20parameter%3CUL%3E%0A%3CLI%3EGo%20to%20the%20Web%20PubSub%20resource%2C%20go%20to%20the%20Keys%20tab%2C%20and%20use%20the%20Client%20URL%20Generator%20to%20generate%20the%20Client%20URL%20which%20contains%20a%20temp%20value%20for%20access_token%3C%2FLI%3E%0A%3CLI%3E%26nbsp%3BMake%20sure%20the%20Hub%20name%20is%20the%20same%20name%20as%20the%20one%20set%20in%20APIM%3C%2FLI%3E%0A%3CLI%3EThe%20%22Client%20Access%20URL%22%20will%20have%20a%20form%20of%3A%20wss%3A%2F%2F%3CYOUR_SVC_NAME%3E.webpubsub.azure.com%2Fclient%2Fhubs%2F%3CYOUR_HUB_NAME%3E%3Faccess_token%3D%3CTOKENTOCOPY%3E%3CUL%3E%0A%3CLI%3ECopy%20the%20value%20of%20the%20access_token%20to%20the%20query%20parameter%20of%20the%20APIM%20and%20click%20Connect.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FTOKENTOCOPY%3E%3C%2FYOUR_HUB_NAME%3E%3C%2FYOUR_SVC_NAME%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22SherrySahni_6-1641539481004.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22SherrySahni_6-1641539481004.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F337918iCA8FCE8328E5EACF%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22SherrySahni_6-1641539481004.png%22%20alt%3D%22SherrySahni_6-1641539481004.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22SherrySahni_7-1641539481018.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22SherrySahni_7-1641539481018.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F337919i5DA05DDBCB68CD33%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22SherrySahni_7-1641539481018.png%22%20alt%3D%22SherrySahni_7-1641539481018.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EWhen%20generating%20url%20(e.g.%20via%20serviceClient.GenerateClientAccessUri)%20make%20sure%20it%20contains%20both%20APIM%20key%20(in%20case%20it%20is%20used%20for%20auth)%20and%20the%20WPS%20access_token%20query%20string%20parameters%3A%3CBR%20%2F%3Ewss%3A%2F%2Fyour_service_name.azure-api.net%2Fclient%2Fhubs%2Fyour_hub_name%3Fsubscription-key%3D%3CAPIM_KEY%3E%26amp%3Baccess_token%3D%3CWPS_KEY%3E%3C%2FWPS_KEY%3E%3C%2FAPIM_KEY%3E%3C%2FLI%3E%0A%3CLI%3EIf%20you%20don't%20want%20to%20(or%20it%20is%20not%20feasible)%20to%20generate%20the%20access_token%20that%20APIM%20would%20just%20normally%20pass%20the%20to%20WebPubSub%20service%2C%20you%20could%20allow%20anonymous%20access%20and%20configure%20private%20VNET%20to%20limit%20access%20to%20WebPubSub%20from%20APIM%20only.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSTRONG%3ECommon%20Scenario%20when%20client%20failed%20to%20establish%20Web%20Socket%20connection%20to%20APIM%20and%20to%20backend%20-%3C%2FSTRONG%3E%3CSTRONG%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSTRONG%3E%3CSTRONG%3EScenario%201%20%3A%20Invalid%20websocket%20upgrade%20request%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EWhen%20we%20see%20the%20above%20error%20message%2C%20we%20will%20need%20to%20check%20the%20followings%3CUL%3E%0A%3CLI%3EConnection%20Request%20must%20have%20the%20following%20three%20headers.%20We%20can%20use%20fiddler%20to%20capture%20request%20and%20confirm%20request%20header%3CUL%3E%0A%3CLI%3EConnection%3A%20Upgrade%3C%2FLI%3E%0A%3CLI%3EUpgrade%3A%20Websocket%3C%2FLI%3E%0A%3CLI%3ESec-WebSocket-Version%3A%2013%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22SherrySahni_8-1641539481047.png%22%20style%3D%22width%3A%20485px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22SherrySahni_8-1641539481047.png%22%20style%3D%22width%3A%20485px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F337920i5F779A65E16AD4A1%2Fimage-dimensions%2F485x211%3Fv%3Dv2%22%20width%3D%22485%22%20height%3D%22211%22%20role%3D%22button%22%20title%3D%22SherrySahni_8-1641539481047.png%22%20alt%3D%22SherrySahni_8-1641539481047.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ERequest%20URL%20endpoint%20must%20exist%20APIM.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CUL%3E%0A%3CLI%3EFor%20example%2C%20my%20websocket%20URL%20endpoint%20in%20APIM%20is%26nbsp%3B%3CSTRONG%3Ewss%3A%2F%2Fdemo-apim-oauth.azure-api.net%2Fwsapiwebscoketdemo%3C%2FSTRONG%3E%2C%20then%20the%20request%20URL%20that%20client%20sends%20cannot%20be%26nbsp%3B%3CSTRONG%3Ewss%3A%2F%2Fdemo-apim-oauth.azure-api.net%2Fwsapiwebscoketdemo%2F%7BextraParam%7D%3C%2FSTRONG%3E%3CSTRONG%20style%3D%22font-family%3A%20inherit%3B%22%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EReference%3C%2FSTRONG%3E%20%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fwebsocket-api%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fwebsocket-api%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EHappy%20Learning!%3CSPAN%3E%26nbsp%3B%3A)%3C%2Fimg%3E%3C%2FSPAN%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-3054085%22%20slang%3D%22en-US%22%3E%3CP%3EWebSocket%20Implementation%20in%20API%20Management%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3054085%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20API%20management%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Jan 07 2022 12:25 AM
Updated by: