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

Dheeraj Awale 21 Reputation points
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.
Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Laxman Reddy Revuri 5,475 Reputation points Microsoft External Staff Moderator
    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.


  2. Raymond Huynh (WICLOUD CORPORATION) 620 Reputation points Microsoft External Staff
    2025-07-29T09:11:16.9966667+00:00

    Hi Dheeraj,

    What you’re experiencing relates to receiving messages from clients using simple WebSocket connections in your backend .NET 8 web API for further processing. This typically occurs when you want to process real-time data server-side, but your clients prefer not to use the Azure Web PubSub SDK and instead rely on standard WebSocket libraries.

    Here’s how you can troubleshoot and resolve it:

    • You can have your backend service connect to Azure Web PubSub using a standard WebSocket client, just like your other clients. Use the Client Access URL (which includes the access token) to establish the connection. This allows your backend to receive and process messages in real time, without needing the Azure SDK.
    • Make sure to define a clear message structure so both your clients and backend understand the data being exchanged.
    • Be aware that by not using the SDK, you’ll need to handle reconnections and group management manually.

    You can also find a detailed walkthrough in our documentation here:

    Azure Web PubSub Documentation

    I hope this helps you get things back on track quickly! If you agree with my suggestion, feel free to interact with the system accordingly!


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.