In Azure Web PubSub, How to receive messages in backend microservice for processing

dheeraj awale 1 Reputation point
2025-01-10T05:55:02.9966667+00:00

Requirement:

My clients are using simple WebSocket connection to send messages. I want to receive these messages inside my backend .NET 8 web api and process them further to send fine-tuned data to some distributed cache services.

From documentation, I only understood that I can use backend to only send the Access URL with token to clients, and Hubs are for dealing with events only (not messages).

I don't see how to receive messages in the backend service itself, but I guess I will need to use 'WebPubSubClient' in backend service itself to receive & process messages.

Doubt:

  • For receiving messages, how to set up a WebSocket connection from backend service?
  • If not, Is it right approach to use Client SDK to receive messages in web api service (probably using it in continuous running hosted service) ?
Azure Web PubSub
Azure Web PubSub
An Azure service that provides real-time messaging for web applications using WebSockets and the publish-subscribe pattern.
82 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,757 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,244 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Laxman Reddy Revuri 1,755 Reputation points Microsoft Vendor
    2025-01-20T09:02:49.3133333+00:00

    Hi @dheeraj awale
    Yes, you can use Azure Web PubSub without the Microsoft SDK by connecting directly via WebSockets. Clients can use any WebSocket library to connect using a Client Access URL, which includes an access token for authentication.
    The URL format is: wss://<service_name>.webpubsub.azure.com/client/hubs/<hub_name>?access_token=<token>.
    Clients can then send and receive messages in real-time, with custom message handling based on the defined packet structure.
    By not using the Azure SDK, you lose features like automatic reconnections and simplified group management, requiring manual handling. Clients must be aware of the message packet structure for proper communication. Additionally, you’ll need to manage scalability and connection handling on your own, as Azure Web PubSub won't handle it for you.

    Please accept as "Yes" if the answer provided is useful , so that you can help others in the community looking for remediation for similar issues.


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.