Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use generic hub in SignalR

I am using SignalR in version 2.1.2. I have noticed there are two public hub classes for me to use, Hub and Hub<T>. The former has an MSDN page which appears to be outdated and the latter lacks any MSDN page at all. I believe the MSDN documentation is not up to date with the latest version of SignalR from Nuget (which I'm using), because sources decompiled with the help of ReSharper show both of the classes inherit from HubBase base class. The MSDN page's Inheritance Hierarchy section shows the Hub class as inheriting from Object and implementing IHub and IDisposable interfaces, however the decompiled sources reveal the aforementioned HubBase base class, implementing the IHub interface which in turn implements IDisposable.

The difference between the non-generic and generic variant of the classes is that the non-generic one's Clients property returns IHubCallerConnectionContext<dynamic> while the generic variant returns typed IHubCallerConnectionContext<T>.

I'd like to have my clients typed, so when I call the client methods from the hub, I'd have proper Intellisense support and strongly-typed arguments. What I struggle with, though, is how to let the Hub know that my client model's method is actually to be invoked in the browser.

This is my TestModel class:

public sealed class TestModel
{
   public String Name { get; set; }
   public void Notify() {}
   public void NotifyComplex(TestModel model) {}
}

With the non-generic hub, I'd just call .Notify() or .Notify(new TestModel() { Name = "sth" }) on the dynamicly bound this.Context.Clients.Client(…) or this.Context.Caller, but with the generic class, when I call these empty methods in similar manner, the browser is not notified at all.

How do you use the generic hub class the way it's supposed to be used?

like image 678
Tomáš Hübelbauer Avatar asked Oct 24 '14 09:10

Tomáš Hübelbauer


People also ask

How many connections SignalR can handle?

In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).

Does SignalR require WebSockets?

SignalR takes advantage of WebSocket, an HTML5 API that enables bi-directional communication between the browser and server. SignalR will use WebSockets under the covers when it's available, and gracefully fall back to other techniques and technologies when it isn't, while the application code remains the same.

How do I send a message to a specific signal SignalR?

public async Task BroadcastToUser(string data, string userId) => await Clients. User(userId). SendAsync("broadcasttouser", data); Remember that when we are sending messages to a user, they will be sent to all connections associated with that user and not just any particular connection.


1 Answers

I've found the answer. The MSDN documentation is not up-to-date as of yet, but the ASP .NET site offers nice SignalR tutorials and one of them covers the typed hubs:

http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#stronglytypedhubs

As the example in the article shows, if you use an interface for the type argument, everything works and you get strongly-typed hub clients whose methods are correctly translated to RPCs. Here's a piece of code I tested this with:

public sealed class TestHub
  : Hub<ITestClient>
{
  public override Task OnConnected()
  {
    this.Clients.Caller.SayHello("Hello from OnConnected!");
    return base.OnConnected();
  }

  public void Hi()
  {
    // Say hello back to the client when client greets the server.
    this.Clients.Caller.SayHello("Well, hello there!");
  }
}

public interface ITestClient
{
  void SayHello(String greeting);
}
like image 132
Tomáš Hübelbauer Avatar answered Sep 27 '22 20:09

Tomáš Hübelbauer