SignalR For Real-Time Web Functionality! (10 Minutes Read)
SignalR For Real-Time Web Functionality! (10 Minutes Read)
NET Core
SignalR In 10 Minutes!
Keivan Damirchi
Include:
● What is SignalR?
● Use cases of SignalR
● Example of SignalR In ASP.NET Core
● Understanding the SignalR Architecture
● Authentication and authorization in SignalR
● SignalR and IoT
● Tips for working with Signalr
What is SignalR?
1
Common Use Cases of
SignalR
Chat Applications
SignalR can be used to build chat applications where
users can communicate in real-time. SignalR makes it
easy to send messages to a group of users or to a
specific user.
Real-time Gaming
Real-time gaming applications can use SignalR to
provide an immersive gaming experience where players
can communicate with each other and receive updates
in real-time.
Collaboration Applications
Applications that require collaboration among
multiple users in real-time can benefit from
SignalR. Examples include document editing and
project management applications.
2
Common Use Cases of
SignalR
Live Dashboards
SignalR can be used to build real-time dashboards that
update automatically as data changes. This is useful in
monitoring applications, where real-time updates are
required to track changes in the system.
Notifications
SignalR can be used to send real-time notifications to
users about updates, events, or changes in the system.
For example, notifications can be sent to users when
a new order is placed, or when a new message is
received.
Tip
Before you start working with SignalR, it's important to
understand the basics of how it works.
3
Using SignalR in
an ASP.NET Core
application
Install the SignalR package
Microsoft.AspNetCore.SignalR
In this example, the hub has a single method called "SendMessage" that
takes a username and a message as parameters. The method sends the
message to all connected clients using the "SendAsync" method, which
sends a message to all clients with a matching method name
("ReceiveMessage" in this case).
4
Using SignalR in
an ASP.NET Core
application
Add the hub to the application
In the "ConfigureServices" method of your "Startup" class, add the
following code to enable SignalR:
services.AddSignalR();
Then, in the "Configure" method, add the following code to map the
"ChatHub" class to a URL:
This will make the "ChatHub" class accessible at the URL "/chathub".
5
Using SignalR in
an ASP.NET Core
application
Call the hub from the client
In your client-side code (e.g. JavaScript), you can connect to the hub
and call the "SendMessage" method like this:
Tip
SignalR supports multiple transports such as
WebSockets, Server-Sent Events (SSE), and long polling.
Each transport has its own advantages and
disadvantages, so it's important to choose the
appropriate one for your application.
6
Understanding the
SignalR Architecture
The architecture of SignalR is designed to simplify the
process of building real-time web applications.
7
Understanding the
SignalR Architecture
The basic flow of SignalR communication is as follows:
2
The SignalR server receives the connection
request, creates a new connection object, and
sends back a connection ID to the client.
4
The server receives the message, and uses the
connection ID to send the message to the
appropriate client.
8
Authentication and
authorization in
SignalR
In an SignalR application, authentication and authorization
are typically implemented using middleware and the Authorize
attribute. Middleware is used to authenticate and authorize
incoming requests, while the Authorize attribute is used to
restrict access to specific SignalR hubs or hub methods based
on user authentication and authorization status.Here's a
simple example of authentication and authorization in ASP.NET
Core SignalR.
9
Authentication and
authorization in
SignalR
Add the [Authorize] attribute to the hub class to require
authentication for all methods, and add the
[Authorize("policyName")] attribute to specific methods to
require authorization based on the defined policy.
This ensures that only authenticated users can access the hub,
and only users with the "Admin" role can call the KickUser method.
10
Authentication and
authorization in
SignalR
Finally, in the client-side JavaScript code, use the
AccessTokenFactory provided by the @microsoft/signalr package
to get the access token for the current user:
This gets the access token from the server-side /Account/Token endpoint,
and passes it to the SignalR connection as an access token factory.
11
SignalR and IoT
SignalR can be used to provide real-time
communication between IoT devices and a server. For
example, imagine a scenario where multiple sensors are
sending data to a server. The server can use SignalR to
push this data to a web or mobile application in
real-time. This allows the application to display the
sensor data in real-time and respond to changes as they
occur.
Imagine you have a fleet of trucks that are equipped with GPS
sensors that send location data to a central server. You want to
build a real-time monitoring application that displays the
location of all the trucks on a map, and updates the map in
real-time as the trucks move around. To accomplish this, you
could use SignalR to establish a real-time connection between
the server and a web-based map application. The GPS data from
the trucks would be sent to the server, which would use SignalR
to push updates to the map application in real-time.
12
SignalR and IoT
Here's an outline of how this could work:
13
Close();
Keivan Damirchi