Practical ASP.NET Core SignalR: Server Hubs

Practical ASP.NET Core SignalR

In this section, I’m going to extend my existing ASP.NET Core application to explore more of what SignalR Server Hubs have to offer.

This blog post is apart of a course that is a complete step-by-setup guide on how to build real-time web applications using ASP.NET Core SignalR. By the end of this course, you’ll be able to build real-world, scalable, production applications using the tools and techniques provided in this course.

If you haven’t already, check out the prior sections of this course.

  1. Course Overview
  2. ASP.NET Core SignalR Overview
  3. Basics

SignalR Server Hubs

A SignalR Server Hub is a primary point at which all communication between client and server occur.  You define methods in a hub that can be called by the client. And in the client code, we can define methods that our hub can call.

We can define methods on our hub that can communicate directly with the specific client (caller) who is making the call to our hub. I’m going to add a new method to our existing MessageHub called SendMessageToCaller

Clients

We can also send data to specific clients. To do so, we simply need to know the ConnectionId of the client we want to send data to.

I’ll add a new method called SendMessageToUser which will accept a connectionId as the first parameter which we will use.

In order for our clients to know about each other, I’m going to override two methods on our Hub. OnConnectedAsync and OnDisconnectedAsync. I’ll use these methods to let all connected clients know when a new client as connected or disconnected. This way the clients/browser can keep track of the ConnectionID’s available, in order to send to specific clients.

Groups

Lastly, we can also define Groups of connections. This giving us the ability to send data to specific groups. This is often useful for multi-tenant applications.

I’ll create two new methods, one method JoinGroup which will be used for our clients to join a specific group. SendMessageToGroup will be used to send data to the specific clients that have already called JoinGroup.

Client

First, I’ll update my Razor Page to contain a new select list to specify who we want to send the message to, All, Myself or PrivateGroup. I’ll also add a button that we will wire-up to call our JoinGroup on our Hub.

Now we will wire up our new UI by adding in SignalR client handlers for UserConnected, UserDisconnected events. I’ll also add UI for the Join button click as well as using the select list to determine which methodto invoke on our Hub.

Get The Course!

You’ve got several options:

  1. Check out my Practical ASP.NET Core SignalR playlist on my CodeOpinion YouTube channel.
  2. Access the full course now by enrolling for free on Teachable.
  3. Follow along with the blog post series here on CodeOpinion.com
    1. Course Overview
    2. ASP.NET Core SignalR Overview
    3. Basics
    4. Server Hubs
    5. Server Concepts (Coming Soon)
    6. Scaling (Coming Soon)

Source Code

All of the source code for this blog post and this course is available the Practical.AspNetCore.SignalR repo on GitHub.

Roundup #29: AWS System Manager, System.CommandLine, AzurePipelines.TestLogger, Windows Sandbox, Ambrosia

Here are the things that caught my eye this week.  I’d love to hear what you found most interesting this week.  Let me know in the comments or on Twitter.

.NET Core configuration provider for AWS Systems Manager

We released a new NuGet package, Amazon.Extensions.Configuration.SystemsManager. This NuGet package simplifies how your application loads the application configuration settings in the AWS Systems Manager Parameter Store into the .NET Core configuration system.
Link: https://aws.amazon.com/blogs/developer/net-core-configuration-provider-for-aws-systems-manager/  

System.CommandLine

This repository contains the code for System.CommandLine, a library for command line parsing, invocation, and rendering of terminal output. For more information, please see our wiki.
Link: https://github.com/dotnet/command-line-api  

AzurePipelines.TestLogger

Announce a new test logger for the Visual Studio Test Platform designed to publish your test results in real-time to Azure Pipelines. This means that you can run dotnet test from your build script on Azure Pipelines and feed your test results directly to the test summary for your build without having to rely on post-processing like the PublishTestResults Azure Pipelines task.
Link: https://daveaglick.com/posts/announcing-azurepipelines-testlogger  

Windows Sandbox

How many times have you downloaded an executable file, but were afraid to run it? Have you ever been in a situation which required a clean installation of Windows, but didn’t want to set up a virtual machine? At Microsoft we regularly encounter these situations, so we developed Windows Sandbox: an isolated, temporary, desktop environment where you can run untrusted software without the fear of lasting impact to your PC. Any software installed in Windows Sandbox stays only in the sandbox and cannot affect your host. Once Windows Sandbox is closed, all the software with all its files and state are permanently deleted.
Link: https://techcommunity.microsoft.com/t5/Windows-Kernel-Internals/Windows-Sandbox/ba-p/301849  

Ambrosia: Robust Distributed Programming Made Easy and Efficient

Ambrosia is a programming language independent approach for authoring and deploying highly robust distributed applications. Ambrosia dramatically lowers development and deployment costs and time to market by automatically providing recovery and high availability.
Link: https://github.com/Microsoft/AMBROSIA  

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.

Practical ASP.NET Core SignalR: Basics

Practical ASP.NET Core SignalRIn this section, I’m going to create a simple ASP.NET Core application that will contain a SignalR Hub, Razor Page, and a JavaScript Client.

This blog post is apart of a course that is a complete step-by-setup guide on how to build real-time web applications using ASP.NET Core SignalR. By the end of this course, you’ll be able to build real-world, scalable, production applications using the tools and techniques provided in this course.

If you haven’t already, check out the prior sections of this course.

  1. Course Overview
  2. ASP.NET Core SignalR Overview

Server Hubs

A hub is a primary point at which all communication between client and server occur.  You define methods in a hub that can be called by the client. And in the client code, we can define methods that our hub can call.

First thing is first, we need to install the SignalR package from NuGet.  The simplest way is to modify your csproj by adding:

<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />

Message Hub

I’m going to create the simplest hub which will just send a message to all connected clients.

Startup.cs

Next, we need to add SignalR to the IServiceCollection in the ConfigureServices method. As well, we configure a route to our message hub in the Configure Method.

SignalR JavaScript Client

First thing will be to get the JavaScript client library from npm.

npm install @aspnet/signalr

From here you will want to copy the client file from node_modules/signalr/dist/browser/signalr.js to your wwwroot/lib/signalr.js

Razor Page

Next, I’m going to create a Razor page that will just contain a simple textarea and button that we will wire up to the SignalR hub.  It will also contain a script tag to reference the signalr.js file and a file we will create next called messages.js

JavaScript

Now they are referencing messages.js, let’s actually create it.  This file will provide the functionality to connect to our SignalR Server Hub, send messages to the hub, and receive messages from the hub.

Get The Course!

You’ve got several options:

  1. Check out my Practical ASP.NET Core SignalR playlist on my CodeOpinion YouTube channel.
  2. Access the full course now by enrolling for free on Teachable.
  3. Follow along with the blog post series here on CodeOpinion.com
    1. Course Overview
    2. ASP.NET Core SignalR Overview
    3. Basics
    4. Server Hubs
    5. Server Concepts (Coming Soon)
    6. Scaling (Coming Soon)

Source Code

All of the source code for this blog post and this course is available the Practical.AspNetCore.SignalR repo on GitHub.