Deploy Websocket API

teaTester 20 Reputation points
2024-01-05T07:47:42.2566667+00:00

Hi,

can you guide me on how to create and deploy websocket API with routes in Azure.

Thanks

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Pinaki Ghatak 5,600 Reputation points Microsoft Employee Volunteer Moderator
    2024-01-05T08:35:28.7566667+00:00

    Hello @teaTester

    We are here to help.

    To create and deploy a WebSocket API with routes in Azure, you will need to follow these steps:

    1. Create a backend service that handles WebSocket connections and messages. You can use any language or framework that supports WebSocket, such as Node.js, Python, C#, or Java. You can also use Azure Web PubSub, a fully managed service that enables you to easily build real-time web applications with WebSocket.
    2. Create a WebSocket API in Azure API Management. This will allow you to manage, secure, and monitor your WebSocket API. You can use the Azure portal, Azure CLI, Azure PowerShell, or other Azure tools to import your WebSocket API. You will need to provide the WebSocket URL of your backend service, and optionally configure policies, operations, and routes for your API.
    3. Test your WebSocket API using the API test consoles in both Azure portal and developer portal. You can also use any WebSocket client tool, such as wscat, to connect to your API and send and receive messages.
    4. Deploy your WebSocket API to a gateway in API Management. You can choose from the default gateway, a self-hosted gateway, or an external gateway. You can also enable Azure Application Insights to collect metrics and logs for your API.

    For more details and examples on how to create and deploy a WebSocket API with routes in Azure, you can refer to these resources:

    I hope this helps. If you have any questions, feel free to ask or if this helps you, please mark this as answered to help further readers in the community


  2. Pinaki Ghatak 5,600 Reputation points Microsoft Employee Volunteer Moderator
    2024-01-07T11:46:14.46+00:00

    Hello @teaTester

    The above examples provided earlier should help you. However, it seems you maybe mixing the API REST Call pattern, and the Publish / subscribe pattern.

    Azure API Management and Azure Service Bus are two different services offered by Microsoft Azure. Azure API Management is a solution for publishing APIs to external and internal customers. It allows you to create consistent and modern API gateways for existing back-end services hosted anywhere, and to analyze and optimize your APIs

    On the other hand, Azure Service Bus is a fully managed enterprise message broker with message queues and publish-subscribe topics. It is used to decouple applications and services from each other, providing benefits such as load-balancing work across competing workers, safely routing and transferring data and control across service and application boundaries, and coordinating transactional work that requires a high-degree of reliability

    In short, Azure API Management is used for managing APIs, while Azure Service Bus is used for reliable cloud messaging between applications and services.

    On the other hand, Azure API Management does not natively support WebSockets. If you need to use WebSockets, you may want to consider using Azure Service Bus.
    In your case, you'd create Topics in Service bus, and many subscribers will subscribe to channels (topic queue with some filters).
    See complete tutorial here:
    https://learn.microsoft.com/en-us/azure/service-bus-messaging/topic-filters

    Also a small example of using websockets with Azure service bus:

    var transportProvider = new AmqpTransportProvider();
    var amqpSettings = new AmqpSettings
    {
        RequireSecureTransport = true,
        TransportProviders = {transportProvider}
    };
    
    var socketSettings = new WebSocketTransportSettings()
    {
        Proxy = new WebProxy() {Address = new Uri("http://myproxy"), BypassProxyOnLocal = false},
        SubProtocol = "https",
        Uri = new Uri($"wss:// {_azureConfiguration.NameSpace}"),
    };
    
    AmqpTransportInitiator transportIntInitiator = new AmqpTransportInitiator(amqpSettings, socketSettings);
    var b = transportIntInitiator.ConnectAsync(TimeSpan.FromSeconds(30), new TransportAsyncCallbackArgs());
    
    ServiceBusConnection s = new ServiceBusConnection($"sb:// {_azureConfiguration.NameSpace}", TransportType.AmqpWebSockets);
    s.TokenProvider = tokenProvider;
    s.OperationTimeout = TimeSpan.FromSeconds(30);
    
    var sender = new MessageSender(s, _azureConfiguration.QueueName);
    string messageBody = $"Message";
    var message = new Message(Encoding.UTF8.GetBytes(messageBody));
    await sender.SendAsync(message);
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.