ASP.NET Core 2.0 & SignalR Core (alpha)

Problem

How to use SignalR Core in ASP.NET Core 2.0 web applications to provide real-time communication.

Scenario: After processing a payroll, the system will trigger a report generation process. Once this process is complete we want to notify all the web clients that their reports are available to view.

Note: I want to demonstrate a real world usage of SignalR but also want to keep it simple enough. Like SignalR, this post is ‘alpha’ too i.e. be gentle in your feedback if you find bugs 😉

Note: I am assuming that you are familiar with previous version of SignalR, if not, please check the documentation here.

Solution

We will build two applications: server and client. Server will receive notifications from the reporting engine when reports are ready and in turn it will notify the clients.

Server

Create an empty console application (.NET Core)  and add NuGet packages:

  • Microsoft.AspNetCore.All
  • Microsoft.AspNetCore.SignalR

Update Program class:

Add a Startup class to add services and middleware for SignalR:

Create a class that inherits from SignalR Hub class:

Client

Create an empty ASP.NET Core 2.0 web application and add NuGet package Microsoft.AspNetCore.SignalR. Update the Startup class to add services and middleware for MVC:

Add three controllers (Home, Publisher, Reports) each returning Index view e.g.:

Note: Download the JavaScript files for SignalR and copy in wwwroot/js folder. You can find these in the source code for project that accompanies this post.

Add a layout view:

Add Index view for Publisher controller:

Add Index view for Reports controller:

Run both the server and client applications. You could open multiple instances of browser windows and observe the output:

Note: Publisher controller acts as the reporting engine that notifies the server of report completion.

Source Code

GitHub: https://github.com/TahirNaushad/Fiver.Asp.SignalR

3 comments

  1. Hi, Could you please demonstrate how to publish an event from a controller method. Eg when new data is stored to the database then you broadcast the new data to all devices listening. Thanks in advance.

  2. Hi Tahir,

    Thanks for the quick response. I went through the post and it was a bit too complex for my understanding. How I got it to work is a bit odd and I’m trying to understand why it works this way. Perhaps you can help me out. Pardon me, I’m fresh from a PHP background :-).

    So this is what I did. First I merged the two projects into one project by shifting the code in Startup.cs to that of the other project and copied ReportsPublisher.cs over as well. Then in the HomeController I added a new function that invokes a broadcast. I saw this on StackOverflow -> https://stackoverflow.com/a/46319153/5372781

    public class HomeController : Controller
    {
    private readonly IHubContext _hubContext;

    public HomeController(IHubContext hubContext)
    {
    _hubContext = hubContext;
    }

    public string SendToAll()
    {
    _hubContext.Clients.All.InvokeAsync(“OnReportPublished”, “Hello”);
    return “Sent”;
    }
    }

    then in the views I changed the url to as follows

    let hubUrl = ‘/reportsPublisher’;

    This way, when I load the ~/home/sendtoall page on the browser, all connected clients get updated.

    I was unable to get DI to work across the two projects. Any tips on how to make that work?

Leave a Reply