2018-03-06 06:13:57 +08:00
---
2018-10-04 04:04:56 +08:00
title: Get started with ASP.NET Core SignalR
2018-07-17 04:09:39 +08:00
author: tdykstra
2018-10-04 04:04:56 +08:00
description: In this tutorial, you create a chat app that uses ASP.NET Core SignalR.
2018-04-11 02:34:30 +08:00
monikerRange: '>= aspnetcore-2.1'
2018-07-17 04:09:39 +08:00
ms.author: tdykstra
2018-03-10 11:31:50 +08:00
ms.custom: mvc
2018-09-05 02:45:15 +08:00
ms.date: 08/31/2018
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
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
2018-08-09 03:14:06 +08:00
![SignalR sample app ](signalr/_static/signalr-get-started-finished.png )
2018-03-15 22:44:09 +08:00
2018-10-27 12:34:40 +08:00
[View or download sample code ](https://github.com/aspnet/Docs/tree/master/aspnetcore/tutorials/signalr/sample ) ([how to download](xref:index#how-to-download-a-sample)).
2018-03-06 06:13:57 +08:00
2018-06-27 03:46:21 +08:00
## Prerequisites
2018-03-06 06:13:57 +08:00
2018-03-15 22:44:09 +08:00
# [Visual Studio](#tab/visual-studio)
2018-09-05 02:45:15 +08:00
* [Visual Studio 2017 version 15.8 or later ](https://www.visualstudio.com/downloads/ ) with the **ASP.NET and web development** workload
2018-05-31 05:32:45 +08:00
* [.NET Core SDK 2.1 or later ](https://www.microsoft.com/net/download/all )
2018-03-15 22:44:09 +08:00
# [Visual Studio Code](#tab/visual-studio-code)
2018-04-17 06:55:31 +08:00
* [Visual Studio Code ](https://code.visualstudio.com/download )
2018-08-09 03:14:06 +08:00
* [.NET Core SDK 2.1 or later ](https://www.microsoft.com/net/download/all )
2018-03-15 22:44:09 +08:00
* [C# for Visual Studio Code ](https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp )
2018-03-06 06:13:57 +08:00
2018-08-11 04:20:10 +08:00
# [Visual Studio for Mac](#tab/visual-studio-mac)
* [Visual Studio for Mac version 7.5.4 or later ](https://www.visualstudio.com/downloads/ )
* [.NET Core SDK 2.1 or later ](https://www.microsoft.com/net/download/all ) (included in the Visual Studio install)
2018-08-09 03:14:06 +08:00
---
2018-03-15 22:44:09 +08:00
2018-10-04 04:04:56 +08:00
## Create a web project
2018-03-06 06:13:57 +08:00
2018-04-17 06:55:31 +08:00
# [Visual Studio](#tab/visual-studio/)
2018-08-09 03:14:06 +08:00
* From the menu, select **File > New Project** .
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
* In the **New Project** dialog, select **Installed > Visual C# > Web > ASP.NET Core Web Application** . Name the project *SignalRChat* .
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
![New Project dialog in Visual Studio ](signalr/_static/signalr-new-project-dialog.png )
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
* Select **Web Application** to create a project that uses Razor Pages.
2018-03-06 06:13:57 +08:00
2018-08-21 01:16:41 +08:00
* Select a target framework of ** .NET Core**, select **ASP.NET Core 2.1** , and click **OK** .
2018-03-15 22:44:09 +08:00
2018-08-09 03:14:06 +08:00
![New Project dialog in Visual Studio ](signalr/_static/signalr-new-project-choose-type.png )
2018-03-15 22:44:09 +08:00
2018-08-09 03:14:06 +08:00
# [Visual Studio Code](#tab/visual-studio-code/)
2018-04-19 23:47:02 +08:00
2018-08-09 03:14:06 +08:00
* Open a folder that you can use for a new project.
2018-03-15 22:44:09 +08:00
2018-08-09 03:14:06 +08:00
* In the **Integrated Terminal** , run the following command:
2018-04-17 06:55:31 +08:00
2018-08-09 03:14:06 +08:00
```console
dotnet new webapp -o SignalRChat
```
2018-04-05 07:51:35 +08:00
2018-08-11 04:20:10 +08:00
# [Visual Studio for Mac](#tab/visual-studio-mac)
* From the menu, select **File > New Solution** .
* Select ** .NET Core > App > ASP.NET Core Web App** (Don't select **ASP.NET Core Web App (MVC)** ).
* Select **Next** .
* Name the project *SignalRChat* , and then select **Create** .
2018-08-09 03:14:06 +08:00
---
## Add the SignalR client library
2018-06-09 09:02:20 +08:00
2018-10-04 04:04:56 +08:00
The SignalR server library is included in the `Microsoft.AspNetCore.App` metapackage. 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.
2018-08-09 03:14:06 +08:00
# [Visual Studio](#tab/visual-studio/)
2018-03-06 06:13:57 +08:00
2018-09-05 02:45:15 +08:00
* In **Solution Explorer** , right-click the project, and select **Add** > **Client-Side Library** .
2018-08-09 03:14:06 +08:00
2018-09-05 02:45:15 +08:00
* In the **Add Client-Side Library** dialog, for **Provider** select **unpkg** .
2018-08-09 03:14:06 +08:00
2018-10-04 02:08:04 +08:00
* For **Library** , enter `@aspnet/signalr@1` , and select the latest version that isn't preview.
2018-03-06 06:13:57 +08:00
2018-09-05 02:45:15 +08:00
![Add Client-Side Library dialog - select library ](signalr/_static/libman1.png )
2018-08-09 03:14:06 +08:00
2018-09-05 02:45:15 +08:00
* Select **Choose specific files** , expand the *dist/browser* folder, and select *signalr.js* and *signalr.min.js* .
2018-04-29 21:15:13 +08:00
2018-09-05 02:45:15 +08:00
* Set **Target Location** to *wwwroot/lib/signalr/* , and select **Install** .
2018-08-11 04:20:10 +08:00
2018-09-05 02:45:15 +08:00
![Add Client-Side Library dialog - select files and destination ](signalr/_static/libman2.png )
2018-08-11 04:20:10 +08:00
2018-10-04 04:04:56 +08:00
LibMan creates a *wwwroot/lib/signalr* folder and copies the selected files to it.
2018-09-05 02:45:15 +08:00
# [Visual Studio Code](#tab/visual-studio-code/)
2018-04-17 06:55:31 +08:00
2018-09-05 02:45:15 +08:00
* In the **Integrated Terminal** , run the following command to install LibMan.
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
```console
2018-09-05 02:45:15 +08:00
dotnet tool install -g Microsoft.Web.LibraryManager.Cli
2018-08-09 03:14:06 +08:00
```
2018-03-06 06:13:57 +08:00
2018-09-05 02:45:15 +08:00
* 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. You might have to wait a few seconds before seeing output.
2018-04-17 06:55:31 +08:00
2018-08-09 03:14:06 +08:00
```console
2018-09-27 22:38:19 +08:00
libman install @aspnet/signalr -p unpkg -d wwwroot/lib/signalr --files dist/browser/signalr.js --files dist/browser/signalr.min.js
2018-08-09 03:14:06 +08:00
```
2018-03-06 06:13:57 +08:00
2018-09-05 02:45:15 +08:00
The parameters specify the following options:
* Use the unpkg provider.
* Copy files to the *wwwroot/lib/signalr* destination.
* Copy only the specified files.
The output looks like the following example:
2018-03-15 22:44:09 +08:00
2018-08-09 03:14:06 +08:00
```console
2018-09-05 02:45:15 +08:00
wwwroot/lib/signalr/dist/browser/signalr.js written to disk
wwwroot/lib/signalr/dist/browser/signalr.min.js written to disk
2018-09-27 22:38:19 +08:00
Installed library "@aspnet/signalr@1.0.3" to "wwwroot/lib/signalr"
2018-08-09 03:14:06 +08:00
```
2018-04-11 03:16:52 +08:00
2018-09-05 02:45:15 +08:00
# [Visual Studio for Mac](#tab/visual-studio-mac)
* In the **Terminal** , run the following command to install LibMan.
2018-03-15 22:44:09 +08:00
2018-09-05 02:45:15 +08:00
```console
dotnet tool install -g Microsoft.Web.LibraryManager.Cli
2018-08-09 03:14:06 +08:00
```
2018-08-10 05:51:00 +08:00
2018-09-05 02:45:15 +08:00
* 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.
```console
2018-09-27 22:38:19 +08:00
libman install @aspnet/signalr -p unpkg -d wwwroot/lib/signalr --files dist/browser/signalr.js --files dist/browser/signalr.min.js
2018-08-09 03:14:06 +08:00
```
2018-03-15 22:44:09 +08:00
2018-09-05 02:45:15 +08:00
The parameters specify the following options:
* Use the unpkg provider.
* Copy files to the *wwwroot/lib/signalr* destination.
* Copy only the specified files.
2018-03-15 22:44:09 +08:00
2018-09-05 02:45:15 +08:00
The output looks like the following example:
2018-03-15 22:44:09 +08:00
2018-09-05 02:45:15 +08:00
```console
wwwroot/lib/signalr/dist/browser/signalr.js written to disk
wwwroot/lib/signalr/dist/browser/signalr.min.js written to disk
2018-09-27 22:38:19 +08:00
Installed library "@aspnet/signalr@1.0.3" to "wwwroot/lib/signalr"
2018-09-05 02:45:15 +08:00
```
---
2018-03-06 06:13:57 +08:00
2018-10-04 04:04:56 +08:00
## Create a SignalR hub
2018-03-06 06:13:57 +08:00
2018-10-04 04:04:56 +08:00
A *hub* is a class that serves as a high-level pipeline that handles client-server communication.
2018-04-17 06:55:31 +08:00
2018-08-09 03:14:06 +08:00
* In the SignalRChat project folder, create a *Hubs* folder.
2018-03-15 22:44:09 +08:00
2018-08-09 03:14:06 +08:00
* In the *Hubs* folder, create a *ChatHub.cs* file with the following code:
[!code-csharp[Startup ](signalr/sample/Hubs/ChatHub.cs )]
2018-10-04 04:04:56 +08:00
The `ChatHub` class inherits from the SignalR `Hub` class. The `Hub` class manages connections, groups, and messaging.
2018-08-09 03:14:06 +08:00
The `SendMessage` method can be called by any connected client. It sends the received message to all clients. SignalR code is asynchronous to provide maximum scalability.
2018-03-06 06:13:57 +08:00
2018-10-04 04:04:56 +08:00
## Configure SignalR
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
The SignalR server must be configured to pass SignalR requests to SignalR.
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
* Add the following highlighted code to the *Startup.cs* file.
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
[!code-csharp[Startup ](signalr/sample/Startup.cs?highlight=7,33,52-55 )]
2018-03-06 06:13:57 +08:00
2018-10-04 04:04:56 +08:00
These changes add SignalR to the ASP.NET Core dependency injection system and the middleware pipeline.
2018-03-06 06:13:57 +08:00
2018-10-04 04:04:56 +08:00
## Add SignalR client code
2018-03-06 06:13:57 +08:00
2018-09-05 02:45:15 +08:00
* Replace the content in *Pages\Index.cshtml* with the following code:
2018-06-06 21:10:36 +08:00
2018-08-09 03:14:06 +08:00
[!code-cshtml[Index ](signalr/sample/Pages/Index.cshtml )]
2018-06-06 21:10:36 +08:00
2018-08-09 03:14:06 +08:00
The preceding code:
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
* Creates text boxes for name and message text, 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* application code that you create in the next step.
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
* In the *wwwroot/js* folder, create a *chat.js* file with the following code:
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
[!code-javascript[Index ](signalr/sample/wwwroot/js/chat.js )]
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
The preceding code:
2018-03-15 22:44:09 +08:00
2018-08-09 03:14:06 +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.
2018-03-06 06:13:57 +08:00
2018-08-09 03:14:06 +08:00
## Run the app
2018-03-06 06:13:57 +08:00
2018-08-11 04:20:10 +08:00
# [Visual Studio](#tab/visual-studio)
* Press **CTRL+F5** to run the app without debugging.
# [Visual Studio Code](#tab/visual-studio-code)
2018-08-09 03:14:06 +08:00
* Press **CTRL+F5** to run the app without debugging.
2018-03-06 06:13:57 +08:00
2018-08-11 04:20:10 +08:00
# [Visual Studio for Mac](#tab/visual-studio-mac)
* From the menu, select **Run > Start Without Debugging** .
---
2018-08-09 03:14:06 +08:00
* Copy the URL from the address bar, open another browser instance or tab, and paste the URL in the address bar.
2018-03-15 22:44:09 +08:00
2018-08-09 03:14:06 +08:00
* Choose either browser, enter a name and message, and select the **Send** button.
2018-03-15 22:44:09 +08:00
2018-08-09 03:14:06 +08:00
The name and message are displayed on both pages instantly.
2018-03-15 22:44:09 +08:00
2018-08-09 03:14:06 +08:00
![SignalR sample app ](signalr/_static/signalr-get-started-finished.png )
2018-03-15 22:44:09 +08:00
2018-08-09 03:14:06 +08:00
> [!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.js* in 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.
> ![signalr.js not found error](signalr/_static/f12-console.png)
2018-08-09 06:38:21 +08:00
## Next steps
2018-03-15 22:44:09 +08:00
2018-10-04 04:04:56 +08:00
In this tutorial, you learned how to:
> [!div class="checklist"]
> * Create a web app project.
> * Add the SignalR client library.
> * Create a SignalR hub.
> * Configure the project to use SignalR.
> * Add code that uses the hub to send messages from any client to all connected clients.
2018-03-28 21:33:06 +08:00
2018-10-04 04:04:56 +08:00
To learn more about SignalR, see the introduction:
2018-03-28 21:33:06 +08:00
2018-10-04 04:04:56 +08:00
> [!div class="nextstepaction"]
> [Introduction to ASP.NET Core SignalR](xref:signalr/introduction)