AspNetCore.Docs/aspnetcore/signalr/introduction.md

60 lines
3.4 KiB
Markdown
Raw Normal View History

---
title: Introduction to ASP.NET Core SignalR
2019-01-24 04:36:13 +08:00
author: bradygaster
description: Learn how the ASP.NET Core SignalR library simplifies adding real-time functionality to apps.
2018-04-11 02:34:30 +08:00
monikerRange: '>= aspnetcore-2.1'
2019-01-24 04:36:13 +08:00
ms.author: bradyg
ms.custom: mvc
ms.date: 11/27/2019
no-loc: [Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
uid: signalr/introduction
---
# Introduction to ASP.NET Core SignalR
## What is SignalR?
ASP.NET Core SignalR is an open-source library that simplifies adding real-time web functionality to apps. Real-time web functionality enables server-side code to push content to clients instantly.
Good candidates for SignalR:
* Apps that require high frequency updates from the server. Examples are gaming, social networks, voting, auction, maps, and GPS apps.
* Dashboards and monitoring apps. Examples include company dashboards, instant sales updates, or travel alerts.
* Collaborative apps. Whiteboard apps and team meeting software are examples of collaborative apps.
* Apps that require notifications. Social networks, email, chat, games, travel alerts, and many other apps use notifications.
SignalR provides an API for creating server-to-client [remote procedure calls (RPC)](https://wikipedia.org/wiki/Remote_procedure_call). The RPCs call JavaScript functions on clients from server-side .NET Core code.
Here are some features of SignalR for ASP.NET Core:
* Handles connection management automatically.
* Sends messages to all connected clients simultaneously. For example, a chat room.
* Sends messages to specific clients or groups of clients.
* Scales to handle increasing traffic.
2021-03-10 12:40:27 +08:00
The source is hosted in a [SignalR repository on GitHub](https://github.com/dotnet/AspNetCore/tree/main/src/SignalR).
## Transports
SignalR supports the following techniques for handling real-time communication (in order of graceful fallback):
* [WebSockets](https://tools.ietf.org/html/rfc7118)
* Server-Sent Events
* Long Polling
SignalR automatically chooses the best transport method that is within the capabilities of the server and client.
## Hubs
SignalR uses *hubs* to communicate between clients and servers.
A hub is a high-level pipeline that allows a client and server to call methods on each other. SignalR handles the dispatching across machine boundaries automatically, allowing clients to call methods on the server and vice versa. You can pass strongly-typed parameters to methods, which enables model binding. SignalR provides two built-in hub protocols: a text protocol based on JSON and a binary protocol based on [MessagePack](https://msgpack.org/). MessagePack generally creates smaller messages compared to JSON. Older browsers must support [XHR level 2](https://caniuse.com/#feat=xhr2) to provide MessagePack protocol support.
Hubs call client-side code by sending messages that contain the name and parameters of the client-side method. Objects sent as method parameters are deserialized using the configured protocol. The client tries to match the name to a method in the client-side code. When the client finds a match, it calls the method and passes to it the deserialized parameter data.
## Additional resources
* [Get started with SignalR for ASP.NET Core](xref:tutorials/signalr)
* [Supported Platforms](xref:signalr/supported-platforms)
* [Hubs](xref:signalr/hubs)
* [JavaScript client](xref:signalr/javascript-client)