2018-03-06 06:13:57 +08:00
---
2018-10-04 04:04:56 +08:00
title: Get started with ASP.NET Core SignalR
2019-01-24 04:36:13 +08:00
author: bradygaster
2018-10-04 04:04:56 +08:00
description: In this tutorial, you create a chat app that uses ASP.NET Core SignalR.
2022-12-02 04:29:45 +08:00
<!-- ms.author: bradyg -->
ms.author: wpickett
2021-11-18 02:11:25 +08:00
monikerRange: '>= aspnetcore-3.1'
2023-04-04 01:24:33 +08:00
ms.custom: mvc, engagement-fy23
2023-11-08 04:26:01 +08:00
ms.date: 11/06/2023
2018-06-19 04:39:53 +08:00
uid: tutorials/signalr
2018-10-04 04:04:56 +08:00
# Customer intent: As a developer, I want to get a quick proof-of-concept app running, so I can get a practical introduction to ASP.NET Core SignalR.
2018-03-06 06:13:57 +08:00
---
2018-08-11 04:20:10 +08:00
2018-10-04 04:04:56 +08:00
# Tutorial: Get started with ASP.NET Core SignalR
2018-03-06 06:13:57 +08:00
2023-04-05 09:42:31 +08:00
[!INCLUDE[ ](~/includes/not-latest-version.md )]
2023-03-22 06:01:19 +08:00
2023-10-27 06:46:35 +08:00
:::moniker range=">= aspnetcore-8.0"
2019-10-17 10:57:52 +08:00
2018-08-09 03:14:06 +08:00
This tutorial teaches the basics of building a real-time app using SignalR. You learn how to:
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
> [!div class="checklist"]
2018-10-26 07:14:21 +08:00
> * Create a web project.
2018-10-04 04:04:56 +08:00
> * Add the SignalR client library.
> * Create a SignalR hub.
> * Configure the project to use SignalR.
2018-10-26 07:14:21 +08:00
> * Add code that sends messages from any client to all connected clients.
2018-03-06 06:13:57 +08:00
2018-08-21 01:16:41 +08:00
At the end, you'll have a working chat app:
2018-03-06 06:13:57 +08:00
2023-03-11 07:01:52 +08:00
![SignalR sample app ](~/tutorials/signalr/_static/7.x/signalr-get-started-finished.png )
2018-03-15 22:44:09 +08:00
2019-07-04 00:11:34 +08:00
## Prerequisites
# [Visual Studio](#tab/visual-studio)
2023-10-27 06:46:35 +08:00
[!INCLUDE[ ](~/includes/net-prereqs-vs-8.0.md )]
2019-07-04 00:11:34 +08:00
# [Visual Studio Code](#tab/visual-studio-code)
2023-10-27 06:46:35 +08:00
[!INCLUDE[ ](~/includes/net-prereqs-vsc-8.0.md )]
2019-07-04 00:11:34 +08:00
# [Visual Studio for Mac](#tab/visual-studio-mac)
2023-10-27 06:46:35 +08:00
[!INCLUDE[ ](~/includes/net-prereqs-mac-8.0.md )]
2019-07-04 00:11:34 +08:00
---
2018-03-15 22:44:09 +08:00
2019-07-10 00:35:04 +08:00
## Create a web app project
2018-03-06 06:13:57 +08:00
2021-11-18 02:11:25 +08:00
# [Visual Studio](#tab/visual-studio)
2018-04-17 06:55:31 +08:00
2023-03-08 04:27:29 +08:00
Start Visual Studio 2022 and select **Create a new project** .
2018-03-15 22:44:09 +08:00
2023-11-08 04:26:01 +08:00
![Create a new project from the start window ](~/tutorials/signalr/_static/8.x/start-window-create-new-project-vs17.8.0.png )
2018-03-15 22:44:09 +08:00
2023-11-16 06:00:52 +08:00
In the **Create a new project** dialog, select **ASP.NET Core Web App (Razor Pages)** , and then select **Next** .
2018-04-19 23:47:02 +08:00
2023-11-16 06:00:52 +08:00
![Create an ASP.NET Core Web App ](~/tutorials/signalr/_static/8.x/new-project-select-vs17.9.0.png )
2021-11-18 02:11:25 +08:00
2023-03-08 04:27:29 +08:00
In the **Configure your new project** dialog, enter `SignalRChat` for **Project name** . It's important to name the project `SignalRChat` , including matching the capitalization, so the namespaces match the code in the tutorial.
2021-11-18 02:11:25 +08:00
2023-03-08 04:27:29 +08:00
Select **Next** .
2021-11-18 02:11:25 +08:00
2023-11-16 06:00:52 +08:00
In the **Additional information** dialog, select ** .NET 8.0 (Long Term Support)** and then select **Create** .
2021-11-18 02:11:25 +08:00
2023-11-16 06:00:52 +08:00
![Additional information ](~/tutorials/signalr/_static/8.x/additional-info-vs17.9.0.png )
2018-08-11 04:20:10 +08:00
# [Visual Studio Code](#tab/visual-studio-code)
2023-09-19 11:46:10 +08:00
The tutorial assumes familiarity with VS Code. For more information, see [Getting started with VS Code ](https://code.visualstudio.com/docs )
2019-10-17 10:57:52 +08:00
2023-09-19 11:46:10 +08:00
* Select **New Terminal** from the **Terminal** menu to open the [integrated terminal ](https://code.visualstudio.com/docs/editor/integrated-terminal ).
* Change to the directory (`cd`) that will contain the project.
* Run the following commands:
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
```dotnetcli
dotnet new webapp -o SignalRChat
code -r SignalRChat
```
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
The `dotnet new` command creates a new Razor Pages project in the `SignalRChat` folder.
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
The `code` command opens the `SignalRChat1 folder in the current instance of Visual Studio Code.
2019-10-17 10:57:52 +08:00
2023-09-19 11:46:10 +08:00
[!INCLUDE[ ](~/includes/vscode-trust-authors-add-assets.md )]
2021-11-17 07:43:07 +08:00
# [Visual Studio for Mac](#tab/visual-studio-mac)
2019-10-17 10:57:52 +08:00
2023-03-11 07:01:52 +08:00
Select **File** > **New Project** .
2019-10-17 10:57:52 +08:00
2023-03-11 07:01:52 +08:00
![macOS New solution ](~/tutorials/signalr/_static/7.x/new_project_vsmac.png )
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
In Visual Studio 2022 for Mac select **Web and Console** > **App** > **Web Application** > **Continue** .
2019-10-17 10:57:52 +08:00
2023-03-11 07:01:52 +08:00
![macOS web app template selection ](~/tutorials/signalr/_static/7.x/web_app_template_vsmac.png )
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
In the **Configure your new Web Application** dialog:
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
* Confirm that **Authentication** is set to **No Authentication** .
2023-03-11 07:01:52 +08:00
* Confirm that **Target framework** is set to the latest .NET 7.x version.
2023-03-08 04:27:29 +08:00
* Select **Continue** .
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
Name the project `SignalRChat` and select **Continue** .
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
---
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
## Add the SignalR client library
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
The SignalR server library is included in the ASP.NET Core shared framework. The JavaScript client library isn't automatically included in the project. For this tutorial, use Library Manager (LibMan) to get the client library from [unpkg ](https://unpkg.com/ ). `unpkg` is a fast, global content delivery network for everything on [npm ](https://www.npmjs.com/ ).
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
# [Visual Studio](#tab/visual-studio/)
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
In **Solution Explorer** , right-click the project, and select **Add** > **Client-Side Library** .
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
In the **Add Client-Side Library** dialog:
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
* Select **unpkg** for **Provider**
2023-03-11 07:01:52 +08:00
* Enter `@microsoft/signalr@latest` for **Library** .
2023-03-08 04:27:29 +08:00
* Select **Choose specific files** , expand the *dist/browser* folder, and select `signalr.js` and `signalr.min.js` .
* Set **Target Location** to `wwwroot/js/signalr/` .
* Select **Install** .
2023-11-08 04:26:01 +08:00
![Add Client-Side Library dialog - select library ](~/tutorials/signalr/_static/8.x/find-signalr-client-libs-select-files-vs17.8.0.png )
2023-03-08 04:27:29 +08:00
LibMan creates a `wwwroot/js/signalr` folder and copies the selected files to it.
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
# [Visual Studio Code](#tab/visual-studio-code/)
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
In the integrated terminal, run the following commands to install LibMan after uninstalling any previous version, if one exists.
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
```dotnetcli
dotnet tool uninstall -g Microsoft.Web.LibraryManager.Cli
dotnet tool install -g Microsoft.Web.LibraryManager.Cli
```
2019-10-17 10:57:52 +08:00
2023-07-28 05:35:42 +08:00
[!INCLUDE[ ](~/includes/dotnet-tool-install-arch-options.md )]
2023-03-11 07:01:52 +08:00
Navigate to the project folder, which contains the `SignalRChat.csproj` file.
2023-03-08 04:27:29 +08:00
Run the following command to get the SignalR client library by using LibMan. It may take a few seconds before displaying output.
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
```console
2023-06-14 22:20:50 +08:00
libman install @microsoft/signalr@latest -p unpkg -d wwwroot/js/signalr --files dist/browser/signalr.js
2023-03-08 04:27:29 +08:00
```
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
The parameters specify the following options:
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
* Use the unpkg provider.
* Copy files to the `wwwroot/js/signalr` destination.
* Copy only the specified files.
2019-10-17 10:57:52 +08:00
2023-11-06 20:16:35 +08:00
The output looks similar to the following:
2023-03-08 04:27:29 +08:00
```console
2023-11-06 20:16:35 +08:00
Downloading file https://unpkg.com/@microsoft/signalr@latest/dist/browser/signalr.js...
2023-03-08 04:27:29 +08:00
wwwroot/js/signalr/dist/browser/signalr.js written to disk
Installed library "@microsoft/signalr@latest" to "wwwroot/js/signalr"
```
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
# [Visual Studio for Mac](#tab/visual-studio-mac)
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
In the **Terminal** , run the following commands to install LibMan after uninstalling any previous version, if one exists.
2021-12-14 07:58:36 +08:00
2023-03-08 04:27:29 +08:00
```dotnetcli
dotnet tool uninstall -g Microsoft.Web.LibraryManager.Cli
dotnet tool install -g Microsoft.Web.LibraryManager.Cli
```
2019-10-17 10:57:52 +08:00
2023-07-28 05:35:42 +08:00
[!INCLUDE[ ](~/includes/dotnet-tool-install-arch-options.md )]
2023-03-11 07:01:52 +08:00
Navigate to the project folder, which contains the `SignalRChat.csproj` file.
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
Run the following command to get the SignalR client library by using LibMan:
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
```console
libman install @microsoft/signalr@latest -p unpkg -d wwwroot/js/signalr --files dist/browser/signalr.js --files dist/browser/signalr.js
```
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
The parameters specify the following options:
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
* Use the unpkg provider.
* Copy files to the `wwwroot/js/signalr` destination.
* Copy only the specified files.
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
The output looks like the following example:
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
```console
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"
```
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
---
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
## Create a SignalR hub
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
A *hub* is a class that serves as a high-level pipeline that handles client-server communication.
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
In the SignalRChat project folder, create a `Hubs` folder.
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
In the `Hubs` folder, create the `ChatHub` class with the following code:
2019-10-17 10:57:52 +08:00
2023-11-08 04:26:01 +08:00
[!code-csharp[ChatHub ](~/tutorials/signalr/samples/8.x/SignalRChat/Hubs/ChatHub.cs )]
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
The `ChatHub` class inherits from the SignalR < xref:Microsoft.AspNetCore.SignalR.Hub > class. The `Hub` class manages connections, groups, and messaging.
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
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.
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
## Configure SignalR
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
The SignalR server must be configured to pass SignalR requests to SignalR. Add the following highlighted code to the `Program.cs` file.
2019-10-17 10:57:52 +08:00
2023-11-08 04:26:01 +08:00
[!code-csharp[Startup ](~/tutorials/signalr/samples/8.x/SignalRChat/Program.cs?highlight=1,7,27 )]
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
The preceding highlighted code adds SignalR to the ASP.NET Core dependency injection and routing systems.
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
## Add SignalR client code
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
Replace the content in `Pages/Index.cshtml` with the following code:
2019-10-17 10:57:52 +08:00
2023-11-08 04:26:01 +08:00
[!code-cshtml[Index ](~/tutorials/signalr/samples/8.x/SignalRChat/Pages/Index.cshtml )]
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
The preceding markup:
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
* Creates text boxes and a submit button.
* Creates a list with `id="messagesList"` for displaying messages that are received from the SignalR hub.
* Includes script references to SignalR and the `chat.js` app code is created in the next step.
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
In the `wwwroot/js` folder, create a `chat.js` file with the following code:
2019-10-17 10:57:52 +08:00
2023-11-08 04:26:01 +08:00
[!code-javascript[chat ](~/tutorials/signalr/samples/8.x/SignalRChat/wwwroot/js/chat.js )]
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
The preceding JavaScript:
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
* Creates and starts a connection.
* Adds to the submit button a handler that sends messages to the hub.
* Adds to the connection object a handler that receives messages from the hub and adds them to the list.
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
## Run the app
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
# [Visual Studio](#tab/visual-studio)
2019-10-17 10:57:52 +08:00
2023-05-14 19:52:06 +08:00
Select < kbd > Ctrl< / kbd > +< kbd > F5< / kbd > to run the app without debugging.
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
# [Visual Studio Code](#tab/visual-studio-code)
2019-10-17 10:57:52 +08:00
2023-05-14 19:52:06 +08:00
Select < kbd > Ctrl< / kbd > +< kbd > F5< / kbd > to run the app without debugging.
2019-10-17 10:57:52 +08:00
2019-11-22 00:50:47 +08:00
# [Visual Studio for Mac](#tab/visual-studio-mac)
2019-10-17 10:57:52 +08:00
2023-05-14 19:52:06 +08:00
Select **Debug** > **Start Without Debugging** to run the app without debugging.
2019-10-17 10:57:52 +08:00
2019-11-22 00:50:47 +08:00
---
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
Copy the URL from the address bar, open another browser instance or tab, and paste the URL in the address bar.
Choose either browser, enter a name and message, and select the **Send Message** button.
2019-10-17 10:57:52 +08:00
2023-03-08 04:27:29 +08:00
The name and message are displayed on both pages instantly.
2023-03-11 07:01:52 +08:00
![Completed SignalR sample app ](~/tutorials/signalr/_static/7.x/signalr-get-started-finished.png )
2019-10-17 10:57:52 +08:00
2021-11-17 07:43:07 +08:00
> [!TIP]
2023-03-08 04:27:29 +08:00
> If the app doesn't work, open the browser developer tools (F12) and go to the console. Look for possible errors related to HTML and JavaScript code. For example, if `signalr.js` was put in a different folder than directed, the reference to that file won't work resulting in a 404 error in the console.
2023-03-11 07:01:52 +08:00
> ![signalr.js not found error](~/tutorials/signalr/_static/7.x/f12-console.png)
2023-03-08 04:27:29 +08:00
> If an `ERR_SPDY_INADEQUATE_TRANSPORT_SECURITY` error has occurred in Chrome, run the following commands to update the development certificate:
2021-11-17 07:43:07 +08:00
>
2023-03-08 04:27:29 +08:00
> ```dotnetcli
> dotnet dev-certs https --clean
> dotnet dev-certs https --trust
> ```
2019-10-17 10:57:52 +08:00
2022-04-20 04:55:07 +08:00
## Publish to Azure
2023-03-08 04:27:29 +08:00
For information on deploying to Azure, see [Quickstart: Deploy an ASP.NET web app ](/azure/app-service/quickstart-dotnetcore ). For more information on Azure SignalR Service, see [What is Azure SignalR Service? ](/azure/azure-signalr/signalr-overview ).
2022-04-20 04:55:07 +08:00
2023-04-04 01:24:33 +08:00
## Next steps
* [Use hubs ](xref:signalr/hubs )
* [Strongly typed hubs ](xref:signalr/hubs#strongly-typed-hubs )
* [Authentication and authorization in ASP.NET Core SignalR ](xref:signalr/authn-and-authz )
* [View or download sample code ](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/signalr/javascript-client/samples ) ([how to download](xref:index#how-to-download-a-sample))
2022-02-01 07:12:01 +08:00
:::moniker-end
2023-03-08 04:27:29 +08:00
2023-10-27 06:46:35 +08:00
[!INCLUDE[ ](~/tutorials/signalr/includes/signalr7.md )]
2023-03-08 04:27:29 +08:00
[!INCLUDE[ ](~/tutorials/signalr/includes/signalr6.md )]
[!INCLUDE[ ](~/tutorials/signalr/includes/signalr3-5.md )]