1

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!

Lab Lab
  • 781
  • 10
  • 34
  • Looks possible with multiple connections, as described https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/working-with-groups and https://stackoverflow.com/questions/29346882/signalr-connect-to-multiple-servers . Not sure if this is best practice. – PM. Jan 10 '19 at 00:25
  • @PM, hmm...Yes, there are multiple connections , but they still go out from the same single web application. But I'm curious about the way of connection from other separeted desktop application. And the more time I spend for finding the answer, the more I refuse to believe this is even possible. – Lab Lab Jan 10 '19 at 00:50
  • From your desktop application, call MVC application controller function – Tabish Matin Jan 10 '19 at 02:04
  • @LabLab Do you mean the desktop application will be a SignalR client connecting to the existing hub? – Andy Vaal Jan 10 '19 at 12:17
  • @AndyVaal, no, it would be a simple desktop client, which connecting to the existing SignalR hub made for Web application – Lab Lab Jan 10 '19 at 13:45
  • @AndyVaal, I have a web-chat. And now the task is to build a simple desktop application, that would use the same SignalR hub as web. Generally, the question is: how to combine two applications, so the users from web-chat and users from desktop were able to communicate in real-time? – Lab Lab Jan 10 '19 at 13:47

1 Answers1

5

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");
Andy Vaal
  • 493
  • 4
  • 12
  • So do you mean, if I have a method like `hub.server.addNewMessage` written in `js`, which calls method `AddNewMessage()` inside the Hub, I can call this Hub's method the same way, but in `.NET` Desktop App? – Lab Lab Jan 10 '19 at 21:32
  • 1
    @LabLab Yes you can, I've updated the answer to include a code sample. For more information see https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-net-client#callserver – Andy Vaal Jan 11 '19 at 08:19