Lab Lab
Lab Lab

Reputation: 801

Is there a way to connect to SignalR Hub built in ASP. NET from other application?

For implmenting a real-time online chat, I already have some web application using ASP .NET MVC and SignalR. I have SignalR ChatHub : Hub and connect to this hub from the client side like following: let hub = $.connection.chatHub and it's working well. And what if I want to use this hub in desktop application? Is this even possible and is this a good practice? If yes, how do I connect to it? Any Ideas? Thanks!

Upvotes: 1

Views: 5116

Answers (1)

Andy Vaal
Andy Vaal

Reputation: 493

A .NET SignalR client exists that can be used to allow a desktop application to connect to a SignalR hub. It is available from NuGet.

Configure the client to connect to the hub in the same way your web client does and messages will be shared between all connected users regardless of platform.

The SignalR hub and client must be the same version (see here: Connect to SignalR server in dotnet core from .NET client).

.NET 4.5+

For .NET versions 4.5+ (not including .NET Core) you require the older client: Microsoft.AspNet.SignalR.Client

Documentation and code samples are available here: https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-net-client

ASP.NET Core

If your MVC application is using ASP.NET Core then use this package instead: Microsoft.AspNetCore.SignalR.Client

You can find documentation here: https://learn.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-2.2

Example - Calling a Server Method from Desktop Application Client

The following is from the .NET 4.5+ documentation:

// Connect
var hubConnection = new HubConnection("http://www.contoso.com/");
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("stockTicker");
await hubConnection.Start();

// Call server method "JoinGroup" from client
stockTickerHubProxy.Invoke("JoinGroup", "SignalRChatRoom");

Upvotes: 5

Related Questions