6.9 KiB
title | author | description | monikerRange | ms.author | ms.custom | ms.date | uid |
---|---|---|---|---|---|---|---|
Get started with SignalR on ASP.NET Core | rachelappel | In this tutorial, you create an app using SignalR for ASP.NET Core. | >= aspnetcore-2.1 | rachelap | mvc | 05/22/2018 | tutorials/signalr |
Get started with SignalR on ASP.NET Core
By Rachel Appel
This tutorial teaches the basics of building a real-time app using SignalR for ASP.NET Core.
This tutorial demonstrates the following SignalR development tasks:
[!div class="checklist"]
- Create a SignalR on ASP.NET Core web app.
- Create a SignalR hub to push content to clients.
- Modify the
Startup
class and configure the app.
View or download sample code (how to download)
Prerequisites
Install the following software:
Visual Studio
- .NET Core SDK 2.1 or later
- Visual Studio 2017 version 15.7.3 or later with the ASP.NET and web development workload
- npm
Visual Studio Code
Create an ASP.NET Core project that hosts SignalR client and server
Visual Studio
-
Use the File > New Project menu option and choose ASP.NET Core Web Application. Name the project SignalRChat.
-
Select Web Application to create a project using Razor Pages. Then select OK. Be sure that ASP.NET Core 2.1 is selected from the framework selector, though SignalR runs on older versions of .NET.
Visual Studio includes the Microsoft.AspNetCore.SignalR
package containing its server libraries as part of its ASP.NET Core Web Application template. However, the JavaScript client library for SignalR must be installed using npm.
-
Run the following commands in the Package Manager Console window, from the project root:
npm init -y npm install @aspnet/signalr
-
Create a new folder named "signalr" inside the lib folder in your project. Copy the signalr.js file from node_modules\@aspnet\signalr\dist\browser to this folder.
Visual Studio Code
-
From the Integrated Terminal, run the following command:
dotnet new webapp -o SignalRChat
-
Install the JavaScript client library using npm.
npm init -y npm install @aspnet/signalr
-
Create a new folder named "signalr" inside the lib folder in your project. Copy the signalr.js file from node_modules\@aspnet\signalr\dist\browser to this folder.
Create the SignalR Hub
A hub is a class that serves as a high-level pipeline that allows the client and server to call methods on each other.
Visual Studio
-
Add a class to the project by choosing File > New > File and selecting Visual C# Class. Name the class
ChatHub
and the file ChatHub.cs. -
Inherit from
Microsoft.AspNetCore.SignalR.Hub
. TheHub
class contains properties and events for managing connections and groups, as well as sending and receiving data. -
Create the
SendMessage
method that sends a message to all connected chat clients. Notice it returns a Task, because SignalR is asynchronous. Asynchronous code scales better.[!code-csharpStartup]
Visual Studio Code
-
Open the SignalRChat folder in Visual Studio Code.
-
Add a class to the project by selecting File > New File from the menu. Name the class
ChatHub
and the file ChatHub.cs. -
Inherit from
Microsoft.AspNetCore.SignalR.Hub
. TheHub
class contains properties and events for managing connections and groups, as well as sending and receiving data to clients. -
Add a
SendMessage
method to the class. TheSendMessage
method sends a message to all connected chat clients. Notice it returns a Task, because SignalR is asynchronous. Asynchronous code scales better.[!code-csharpStartup]
Configure the project to use SignalR
The SignalR server must be configured so that it knows to pass requests to SignalR.
-
To configure a SignalR project, modify the project's
Startup.ConfigureServices
method.services.AddSignalR
makes the SignalR services available to the dependency injection system. -
Configure routes to your hubs with
UseSignalR
in theConfigure
method.app.UseSignalR
adds SignalR to the middleware pipeline.[!code-csharpStartup]
Create the SignalR client code
-
Add a JavaScript file, named chat.js, to the wwwroot\js folder. Add the following code to it:
[!code-javascriptIndex]
-
Replace the content in Pages\Index.cshtml with the following code:
[!code-cshtmlIndex]
The preceding HTML displays name and message fields, and a submit button. Notice the script references at the bottom: a reference to SignalR and chat.js.
Run the app
Visual Studio
-
Select Debug > Start without debugging to launch a browser and load the website locally. Copy the URL from the address bar.
-
Open another browser instance (any browser) and paste the URL in the address bar.
-
Choose either browser, enter a name and message, and click the Send button. The name and message are displayed on both pages instantly.
Visual Studio Code
-
Press Debug (F5) to build and run the program. Running the program opens a browser window.
-
Open another browser window and load the website locally in it.
-
Choose either browser, enter a name and message, and click the Send button. The name and message are displayed on both pages instantly.