• Skip to primary navigation
  • Skip to main content

Josh Withee

  • Home
  • Blog
  • Contact
  • Show Search
Hide Search

SignalR Quickstart

Josh Withee · August 8, 2020 · 2 Comments

How to set up SignalR in your ASP.NET Core 3.1 project.

Install the Microsoft.AspNetCore.SignalR NuGet package:

Create a Hubs folder in your project, and create MySignalRHub.cs inside of it:

Update MySignalRHub.cs like so:

Use dependency injection to access your DbContext (or whatever you use for accessing your database). In the following image, the UserManager is also injected so that we can determine the user:

Now add the JavaScript file required for using SignalR on your webpages. The Microsoft docs show how to do this with npm, a CDN, and LibMan. I use the LibMan method to download signalr.js (and signalr.min.js):

Add a reference to the JS file (I’m putting it in _Layout.cshtml so that it gets loaded for every page):

Update Startup.cs to add SignalR services and map a URL to your Hub:

Now for the client-side code (I’m just sticking this in Home/Index.cshtml for this example):

This image shows how things are connected:

Related

Uncategorized ASP.NET Core, SignalR

Reader Interactions

Comments

  1. Fredia Raynor Manolo says

    August 23, 2020 at 7:39 am

    Völlig komponiertes Themenmaterial, danke für die selektive Information. Fredia Raynor Manolo

    Reply
    • Josh Withee says

      September 11, 2020 at 10:51 am

      Ich bin froh, dass es hilfreich ist!

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2025