aspnetcore/tutorials/signalr/includes/signalr3-5.md
:::moniker range=">= aspnetcore-3.1 < aspnetcore-6.0"
This tutorial teaches the basics of building a real-time app using SignalR. You learn how to:
[!div class="checklist"]
- Create a web project.
- Add the SignalR client library.
- Create a SignalR hub.
- Configure the project to use SignalR.
- Add code that sends messages from any client to all connected clients.
At the end, you'll have a working chat app:
dotnet new webapp -o SignalRChat
cd SignalRChat
code -r .
The SignalR server library is included in the ASP.NET Core 3.1 shared framework. The JavaScript client library isn't automatically included in the project. For this tutorial, you use Library Manager (LibMan) to get the client library from unpkg. unpkg is a content delivery network (CDN) that can deliver anything found in npm, the Node.js package manager.
@microsoft/signalr@latest.signalr.js and signalr.min.js.LibMan creates a wwwroot/js/signalr folder and copies the selected files to it.
dotnet tool install -g Microsoft.Web.LibraryManager.Cli
libman install @microsoft/signalr@latest -p unpkg -d wwwroot/js/signalr --files dist/browser/signalr.js --files dist/browser/signalr.js
The parameters specify the following options:
The output looks like the following example:
wwwroot/js/signalr/dist/browser/signalr.js written to disk
wwwroot/js/signalr/dist/browser/signalr.js written to disk
Installed library "@microsoft/signalr@latest" to "wwwroot/js/signalr"
dotnet tool install -g Microsoft.Web.LibraryManager.Cli
Navigate to the project folder (the one that contains the SignalRChat.csproj file).
Run the following command to get the SignalR client library by using LibMan.
libman install @microsoft/signalr@latest -p unpkg -d wwwroot/js/signalr --files dist/browser/signalr.js --files dist/browser/signalr.js
The parameters specify the following options:
The output looks like the following example:
wwwroot/js/signalr/dist/browser/signalr.js written to disk
wwwroot/js/signalr/dist/browser/signalr.js written to disk
Installed library "@microsoft/signalr@latest" to "wwwroot/js/signalr"
A hub is a class that serves as a high-level pipeline that handles client-server communication.
ChatHub.cs file with the following code:[!code-csharpChatHub]
The ChatHub class inherits from the SignalR Hub class. The Hub class manages connections, groups, and messaging.
The SendMessage method can be called by a connected client to send a message to all clients. JavaScript client code that calls the method is shown later in the tutorial. SignalR code is asynchronous to provide maximum scalability.
The SignalR server must be configured to pass SignalR requests to SignalR.
Add the following highlighted code to the Startup.cs file.
[!code-csharpStartup]
These changes add SignalR to the ASP.NET Core dependency injection and routing systems.
Replace the content in Pages/Index.cshtml with the following code:
[!code-cshtmlIndex]
The preceding code:
id="messagesList" for displaying messages that are received from the SignalR hub.chat.js application code that you create in the next step.In the wwwroot/js folder, create a chat.js file with the following code:
[!code-javascriptchat]
The preceding code:
In the integrated terminal, run the following command:
dotnet watch run -p SignalRChat.csproj
[!TIP]
If the app doesn't work, open your browser developer tools (F12) and go to the console. You might see errors related to your HTML and JavaScript code. For example, suppose you put
signalr.jsin a different folder than directed. In that case the reference to that file won't work and you'll see a 404 error in the console.If you get the error ERR_SPDY_INADEQUATE_TRANSPORT_SECURITY in Chrome, run these commands to update your development certificate:
dotnetclidotnet dev-certs https --clean dotnet dev-certs https --trust
For information on deploying to Azure, see Quickstart: Deploy an ASP.NET web app.
:::moniker-end