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.
81 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,729 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,185 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 69,121 Reputation points
    2025-01-10T18:53:50.1966667+00:00

    in general a websocket connection is persistent connection rather than a typical http request/response. It is a 2 way stream with no formal packet definition. Part of designing a websocket app is defining the packet structures.

    signal/r is a communication library and protocol built on websockets. It implements common HUBs for the server side and supports packet routing. unless your clients are already using signal/r this is not an option.

    you can easily code to receive raw messages. You code would replace the echo method:

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-9.0

    your clients will need to tell you what the packets structure is and any required protocol packets.

    as hosting a websocket application requires a persistent connections to multiple clients, and typically there are dedicated resources for each connection, which is active for the life of the connection. so a websocket app ties up more resources than a standard website (which typically have a 90 second connection time limit) thus require a different hosting requirement.

    the Azure Web PubSub is an optimized hosting option for websocket applications. but I does not control how the app is developed or tools. it supports .net, node, python, java and azure functions (via hooks).

    0 comments No comments

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.