ASP.NET Core SignalR
Note
This feature is available in NCache Enterprise and Professional editions.
SignalR allows developers to create real-time ASP.NET Core web applications, where the server broadcasts the updates to all registered clients as soon as an update is triggered. This cuts down on the delay caused by the wait for client requests for updates. Similarly ASP.NET Core SignalR is responsible for adding real time functionality to the web applications in a web farm.
Note
For production use, it is recommended to run NCache backplane in the same data center as the SignalR app.
An example of an online chatroom stands valid in this case too. Multiple clients are connected to multiple web servers in a farm. These clients can be sending messages to each other, where the message content is displayed as soon as it is sent to the receiving client. This eliminates the need to refresh the webpage every time to request for new messages, as the server broadcasts the message to all clients as soon as the client state is updated.
ASP.NET Core SignalR is responsible for functionalities like connection persistence and high traffic management. Similarly sending same message to multiple connected clients and sending messages to the specific connected group of clients is to be dealt by ASP.NET Core SignalR.
ASP.NET Core SignalR uses Hubs to communicate between clients and servers. It creates connections between them which ensures message delivery to all clients from their connected servers. NCache lets you use SignalR in your .NET Core Application by providing extension to the SignalR provider.
Using NCache Extension for SignalR Core
NCache extends the ISignalRServerBuilder() method with its AddNCache()
method which requires just
the cache name, event key and user credentials for the item added. This acts as the registration
point for the clients against the ASP.NET Core SignalR implementation.
Pre-Requisites
Please make sure to perform the following steps in order to utilize SignalR Core in your application:
Install the AspNetCore.SignalR.NCache Enterprise/Professional/OpenSource NuGet package to your application by executing the following command in the Package Manager Console:
For Enterprise:
Install-Package AspNetCore.SignalR.NCache
For Professional:
Install-Package AspNetCore.SignalR.NCache.Professional
To utilize the extension, include the following namespaces in your application in Startup.cs:
Alachisoft.NCache.AspNetCore.SignalR
Microsoft.AspNetCore.SignalR
Make sure to use the version 1.1.0 of ASP.NET SignalR Core.
Modify AppSettings.json
Add the following configurations in your appsettings.json file. You have to specify the cache name and the application ID along with the user credentials in NCacheConfiguration
as shown below.
"NCacheConfiguration": {
"CacheName": "demoClusteredCache",
"ApplicationID": "chatApplication",
"UserID": "your-username",
"Password": "your-password"
}
Register Clients to use NCache
Register an instance of the AddNCache()
method in Startup.cs
of your
application as shown in the code below. Please note that NCache provides another overload in case of enabled security where you can provide with security credentials.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<NCacheConfiguration> (Configuration.GetSection("NCacheConfiguration"));
services.AddSignalR().AddNCache(ncacheOptions =>
{
ncacheOptions.CacheName = Configuration["NCacheConfiguration:CacheName"];
ncacheOptions.ApplicationID = Configuration["NCacheConfiguration:ApplicationID"];
// In case of enabled cache security specify the security credentials
ncacheOptions.UserID = Configuration["NCacheConfiguration:UserID"];
ncacheOptions.Password = Configuration["NCacheConfiguration:Password"];
}
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseSignalR(config =>
{
config.MapHub<MessageHub>("/messages");
});
app.UseMvc();
}
}
AddNCache()
takes in the following parameters:
Parameters | Type | Description |
---|---|---|
CacheName |
string |
Name of the cache in NCache which will store the respective item for the client to trace updates via itemVersion . |
ApplicationID |
string |
Unique, user specified key attribute for the item added to NCache on client registration. Event key is specific to the application. Each client of the application will use the same event key while calling NCache's extension method. |
UserId |
string |
User ID credential for the user. |
Password |
string |
Password specified for the user. |
Additional Resources
NCache provides sample application for SignalR on GitHub.
See Also
Object Caching in ASP.NET Core
ASP.NET Core Response Caching
Multi-Region ASP.NET Core Session Provider for NCache