Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 57 ยท Messaging and Integration

Web PubSub, SignalR and Media Services

Add real-time features with Azure SignalR Service and Web PubSub: hubs, groups, client access tokens, upstream events and scaling, plus what happened to Media Services.

Most Azure messaging services in this chapter are about servers talking to servers. Chat rooms, live dashboards, collaborative editors and multiplayer games need something different: the server must push data to browsers the moment it changes, over a long-lived connection. Azure has two managed services for this, Azure SignalR Service and Azure Web PubSub, and this lesson also covers Azure Media Services, which you will meet in older architectures.

A real connection needs a resource and a client, so the Azure commands and server code are read-only. The runnable examples are JavaScript stand-ins that model hubs, groups, permissions and token expiry.

Why a managed service?

A WebSocket connection stays open for minutes or hours. If you serve it from your own web app, every instance holds thousands of open sockets, and a message for one user must be routed to the instance that holds that user's socket. Scaling out needs a backplane that shares messages among instances, and deployments drop all connections when instances restart. A managed service moves the sockets off your app: clients connect to the service, your app talks to the service over a small number of connections, and Azure handles scale-out, load balancing and the backplane.

flowchart LR
    C1["Browser A"] <-->|"WebSocket"| S["Azure SignalR /<br/>Web PubSub"]
    C2["Browser B"] <-->|"WebSocket"| S
    C3["Phone"] <-->|"WebSocket"| S
    S <-->|"few server<br/>connections or webhooks"| A["Your app<br/>(App Service, Functions)"]

Clients hold their connections to the service. Your app stays small and stateless.

Azure SignalR Service

ASP.NET Core SignalR is a library that gives .NET apps (and JavaScript, Java and other clients) a high-level API over WebSockets with automatic fallbacks. Azure SignalR Service is the managed backend for it: you add one line to your app and the service handles the connections. Key ideas:

  • A hub is a class of server methods clients can call; the server can call methods on clients.
  • Clients can be addressed individually, by connection, by user (all their devices) or by group.
  • Service modes: Default (your app hosts the hub, the service proxies the sockets), Serverless (no app server; Azure Functions bindings send messages) and a legacy Classic mode.
C#
// Program.cs (read-only sketch): switch SignalR to the managed service
builder.Services.AddSignalR().AddAzureSignalR();
Terminal
az signalr create --name sr-demo-4821 --resource-group rg-rt \
  --sku Standard_S1 --unit-count 1 --service-mode Default

Pick SignalR Service when your code already uses SignalR or you want its high-level hub model.

Azure Web PubSub

Azure Web PubSub is lower level and language-neutral. Clients use plain WebSocket (with a documented subprotocol that adds join, leave and publish messages), so any language with a WebSocket library can connect. The model is classic publish/subscribe:

  • Clients join groups and send to groups; the service fans each message out to every member.
  • Your server issues each client a client access URL containing a short-lived signed token. The token states the user ID, how long it is valid and the roles (permissions) such as webpubsub.joinLeaveGroup.chat and webpubsub.sendToGroup.chat, which allow joining and publishing only in the group chat.
  • Event handlers forward client events (connect, disconnect, user messages) to your HTTPS endpoint as webhooks, so your server can authorize and react without holding connections.
JavaScript
// Server (read-only sketch): issue a token for one user
import { WebPubSubServiceClient } from "@azure/web-pubsub";

const service = new WebPubSubServiceClient(process.env.WEBPUBSUB_CONNECTION_STRING, "chat");
const token = await service.getClientAccessToken({
  userId: "alice",
  roles: ["webpubsub.joinLeaveGroup.room-1", "webpubsub.sendToGroup.room-1"],
  expirationTimeInMinutes: 30,
});
// Send token.url to the browser, which opens: new WebSocket(token.url, "json.webpubsub.azure.v1")

Choose Web PubSub for a non-.NET stack, for pure fan-out messaging, or when you want serverless with Functions triggers.

Groups and permissions: a stand-in

The stand-in models a hub with groups, token roles and fan-out. A client may join or send only where its roles allow, mirroring the real permission model.

JS Groups, roles and fan-out

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Token expiry: a stand-in

Client tokens are short-lived on purpose. If a token leaks, it stops working soon; a client that needs to stay connected longer must reconnect with a fresh token from your server. The stand-in uses a simulated clock.

JS Short-lived client tokens

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Scaling and cost

Both services are priced by tier and units: each unit supports a number of concurrent connections and a daily message allowance (check current limits on the pricing page). Free tiers exist for development, Standard tiers add an SLA and scale-out. Design for the fact that connections are long-lived: send small messages, avoid broadcasting to everyone when a group will do, and enable a private endpoint when the service must not be public (see Private Endpoints, VPN and ExpressRoute).

Compare with the other messaging services: Service Bus and Event Grid and Event Hubs move data between services, while SignalR and Web PubSub deliver it to end-user clients. They are often chained: an event arrives in Event Grid, a function handles it, and it publishes the update to Web PubSub for live browsers.

Azure Media Services

Azure Media Services was the platform for encoding, packaging and streaming video (video on demand and live) with built-in content protection. Microsoft retired it on 30 June 2024, so you cannot start new work on it, and existing designs that mention Media Services, encoding jobs or streaming endpoints need a migration plan. Current approaches are to use partner video platforms from the Azure Marketplace, serve files from Blob Storage behind a CDN for simple on-demand playback, and use Azure Video Indexer (video analysis such as transcripts and scenes) where you need insights rather than streaming. Always check Microsoft's current guidance before relying on any media service, because this area has changed more than most.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a managed service help with WebSocket-based apps?

    Clients connect to the service, which handles connection scale and routing, leaving your app stateless.
  2. 2. In Web PubSub, what controls whether a client may publish to a group?

    The token issued by your server encodes the user and roles, so the service can enforce permissions.
  3. 3. Which is the better fit for a team with an existing ASP.NET Core SignalR app?

    SignalR Service is the managed backend for the SignalR library with a one-line change.
  4. 4. What is the status of Azure Media Services?

    Microsoft retired Media Services, so use partner platforms, storage plus a CDN, or Video Indexer for analysis.

Summary

  • Real-time features need long-lived connections; Azure SignalR Service and Web PubSub host them so your app can stay small.
  • SignalR Service backs the ASP.NET Core SignalR hub model; Web PubSub is language-neutral publish/subscribe with groups.
  • Servers issue short-lived client tokens with roles; event handlers deliver client events to your HTTPS endpoint.
  • These services push to end-user clients, unlike Service Bus and Event Grid, which connect services.
  • Azure Media Services was retired in June 2024; plan alternatives.

Next: Azure Container Registry.