Expand your application with real-time messaging API capability with Web PubSub and API Management

This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Tech Community.

Businesses everywhere are looking to extend their operations as a digital platform, creating new channels, finding new customers, and driving deeper engagement with existing ones. Now, with the API Management (APIM) and Web PubSub service (AWPS), you are able to expand the real-time messaging capability of your application with a consistent and modern API gateway.

  • Build apps faster and deliver immediate value to your customers through API-first approaches.
  • Transform your legacy services into modern REST-based APIs or WebSocket APIs.
  • Consistent experience to manage APIs (REST and WebSocket) across clouds and on-premises.

Architecture.png

Let’s go through the key steps together and learn how to implement this reference solution:  

  • Create the Azure Web PubSub Service
  • Create the Azure API Management service and configure the WebSocket rules
  • Test it with a live demo

Setup Web PubSub

  1. Follow the steps to create your Web PubSub service which we refer to as AWPS1 in the following sections.
  2. When created, go to the Keys tab for the resource from the Azure portal, and you can see the hostname and connection strings of your resource. There is also a Client URL Generator tool in the portal that it can generate some temporarily valid URLs for your WebSocket clients to connect to. Let's use this tool to generate an URL for our demo with a hub called demoHub. This tool is for quick test purposes, we also provide APIs in various languages to generate the URL for production usage, for example, WebPubSubServiceClient.GetClientAccessUri Method for .NET.
  3. Copy the generated Client Access URL for later use. You can see that this Client Access URL contains:
    • the hostname of the Web PubSub service xxx.webpubsub.azure.com with wss scheme,
    • and the path containing the hub name /client/hubs/demoHub,
    • and a query parameter access_token which is the JWT token that the Web PubSub service uses to validate and authenticate the WebSocket clients. keys.png

       

Setup API Management

  1. Follow the steps to create your API Management service which we refer to as APIM1 in the following sections.
  2. When created, go to the APIs tab for the resource from the Azure portal, click Add API and choose WebSocket
  3. APIM.png

     Create with the following parameters:

    • WebSocket URL: wss://<your_webpubsub_name>.webpubsub.azure.com/client/hubs/demoHub

      1. Replace <your_webpubsub_name> with the name of your AWPS1
    • API URL suffixclient/hubs/demoHubset1.png
  4. Now you are all set, select the API and use the Test tab to test the connection.
    1. Remember the access_token query parameter we get from the AWPS1 Client Access URL? Paste it here as the access_token query parameter, and Connect.
    2. set2.pngYou can now see in the output Connected.set3.png

Try samples

Let's update this simple publish and subscribe message sample to use APIM1, the only thing to change is to let the WebSocket subscriber connect to APIM1 instead.

 

After the subscriber gets the full URL to connect to AWPS1, let's update the URL to connect to APIM, and don't forget to append the subscription-key query parameter if your APIM settings enabled it, for example, in C#, update the code as below:

static async Task Main(string[] args) { // ... var serviceClient = new WebPubSubServiceClient(connectionString, hub); var url = serviceClient.GetClientAccessUri(); // Replace the Host with APIM1's, and append APIM1's subscription-key query parameter if it is enabled var urlBuilder = new UriBuilder(url); // Replace the hostname with APIM's urlBuilder.Host = "<APIM1_host_name>.azure-api.net"; // Get the subscription-key if you enabled it in APIM portal var subscriptionKey = "<the_subscription_key>"; var subscriptionKeyQuery = $"subscription-key={subscriptionKey}"; urlBuilder.Query = string.IsNullOrEmpty(urlBuilder.Query) ? subscriptionKeyQuery : $"{urlBuilder.Query}&{subscriptionKeyQuery}"; var apim1Url = urlBuilder.Uri; // Start the WebSocket connection using (var client = new WebsocketClient(apim1Url)) { // ... } }

REMEMBER: these articles are REPUBLISHED. Your best bet to get a reply is to follow the link at the top of the post to the ORIGINAL post! BUT you're more than welcome to start discussions here:

This site uses Akismet to reduce spam. Learn how your comment data is processed.