Overview

4 Adding order-tracking functionality with SignalR

The chapter introduces a delivery-order tracking application built with Aspire, SignalR, and OpenLayers, aiming to mimic the live, automatically updating experience of modern food and package tracking apps. The core idea is to stream a delivery driver’s changing coordinates from the backend to the browser so the user can watch a map marker move in real time without manually refreshing the page.

To support that behavior, the chapter explains how SignalR provides real-time, bidirectional communication over transports such as WebSockets, with fallback options like server-sent events and long polling. It then shows how to create a SignalR hub for location updates, register it in the backend, and use a background service to simulate periodic driver movement by pushing new latitude and longitude values to all connected clients.

On the frontend, the chapter uses OpenLayers to render an interactive map and Blazor JavaScript interop to initialize the map and place a marker whenever a new location arrives. It also covers the extra setup needed for SignalR client connectivity, service discovery, and interactive rendering in Blazor, ultimately tying together backend streaming and map-based UI into a complete real-time distributed application.

Displaying the marker on the map upon receiving the location update
figure

Summary

  • In this chapter, we’ve built a fully interactive map where a user interface persistently connected to the backend server can receive updated locations.
  • The persistent connection is managed by SignalR, a library built into ASP.NET Core that facilitates two-way real-time communication between client and server.
  • The location is displayed as a marker on an OpenLayers map, which can interact with JavaScript.
  • An important point to remember is that for Blazor to manage interactive map updates, it has to render in an interactive mode.
  • Interactive updates to an OpenLayers map in Blazor can be managed via Blazor JavaScript interop functionality.
  • Any updates to the UI from the Blazor code can be made only after the Razor component has been fully rendered.

FAQ

What is SignalR, and why is it useful for order tracking?SignalR is Microsoft’s real-time communication library for ASP.NET Core. It lets the server push updates to connected clients immediately, which is ideal for order tracking because the browser can receive delivery location changes without polling or manual refreshes.
How does SignalR support real-time communication between the server and browser?SignalR uses a persistent connection so the server and client can communicate in both directions over a single channel. In this chapter, that means the backend can send live location updates to the browser as soon as they occur.
What happens if WebSockets are not available for SignalR?SignalR can fall back to other transport mechanisms, such as server-sent events or long polling. This makes the real-time feature more resilient when WebSockets cannot be used.
What is a SignalR hub?A hub is a high-level API in SignalR that simplifies remote procedure calls between server and client. It allows the server to call client-side methods and clients to call server-side methods in a structured way.
How is the location-tracking hub implemented in this chapter?The chapter creates a LocationHub class that inherits from Hub. It includes an UpdateLocation(latitude, longitude) method that sends a ReceiveLocationUpdate event to all connected clients.
How is the SignalR hub registered in the backend?The backend registers SignalR services with builder.Services.AddSignalR() and maps the hub endpoint with app.MapHub<LocationHub>("/locationHub"). This makes the hub available at a specific URL path.
Why is a background service used to simulate driver movement?A background service lets the application generate location updates automatically over time. In the example, LocationUpdater sends a series of coordinates with delays, simulating a moving delivery driver.
Why does the frontend need Microsoft.AspNetCore.SignalR.Client?The client-side project needs this NuGet package to connect to the SignalR hub. Server-side SignalR comes with ASP.NET Core, but the browser-facing app must explicitly install the client library.
Why is OpenLayers used in the tracking UI?OpenLayers is used to display an interactive map in the browser. It supports map tiles, vector layers, and styling, making it a good fit for showing a live delivery marker that updates as coordinates change.
Why does the Blazor app use @rendermode="InteractiveServer"?The interactive render mode is needed so the Blazor component can run JavaScript interop and respond to live updates. Without it, the lifecycle method that initializes the map and starts the SignalR connection would not run as expected.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building Distributed Applications that Work Building Distributed Applications that Work ebook for free