Skip to content

SignalR Client Hubs

davidfowl edited this page May 9, 2012 · 26 revisions

SignalR .NET Client (Hubs)

Setup

See SignalR Client for instructions on how to get started with SignalR.Client.

HubConnection API

To connect to a hub using SignalR, create a HubConnection with the appropriate url.

  • NOTE: This url will not point to a specific connection. But will instead point to the root of your site.

Example

var connection = new HubConnection("http://mysite/");

If you've changed the default hub url on the server (to something other than /signalr), you can connection to that by doing:

var connection = new HubConnection("http://mysite/myhubs", appendDefaultUrl: false);

Set the appendDefaultUrl to false to stop the default logic from appending /signalr to the url.

IHubProxy CreateProxy(string hubName)

Creates a client side proxy to the hub on the server side.

  • Returns an IHubProxy.
  • NOTE: Breaking change, this needs to be the Short name of the hub.

Example

Server:

public class MyHub : Hub
{
    public Task Send(string message)
    {
        return Clients.addMessage(message);
    }
}

Client:

IHubProxy myHub = connection.CreateProxy("MyHub");

If a HubName is specified, then that takes precedence:

Server

[HubName("My")]
public class MyHub : Hub
{
    public Task Send(string message)
    {
        return Clients.addMessage(message);
    }
}

Client:

IHubProxy myHub = connection.CreateProxy("My");

Task Start()

Starts the connection for all hubs.

IHubProxy API

The hub proxy object can be used to subscribe to events and invoke methods on the server side hub. To create a hub proxy, use HubConnection.CreateProxy.

Subscription Subscribe(string eventName)

Subscribes for notifications on the specified event.

  • Returns a Subscription object.
  • NOTE: Calling create proxy with the same name will return the same Subscription object.
  • NOTE: This API isn't the recommended way to subscribe to events on a hub. There are other extension methods on IHubProxy that provide a cleaner way to do type conversion of incoming parameters.

On Extension methods

To make subscribing to events on hubs easy and convenient, the On methods were created. These provide a clean API for subscribing to events on the hub.

IDisposable On(string eventName, Action onData)

Subscribes to the event name and calls triggers onData when a messages for the specified event name is received.

myHub.On("notify", () => Console.WriteLine("notified!"));

IDisposable On(string eventName, Action<dynamic> onData) (Not on WP7)

Subscribes to the event name and calls triggers onData when a messages for the specified event name is received.

myHub.On("receive", data => Console.WriteLine("Received data {0}", data));

IDisposable On<T>(string eventName, Action<T> onData) (Not on WP7)

Subscribes to the event name and calls triggers onData when a messages for the specified event name is received.

  • NOTE: There are various generic overloads to On which allow specifying up to 8 parameters (4 on WP7).

Example

myHub.On<string>("receive", data => Console.WriteLine("Received data {0}", data));

Task Invoke(string action, params object[] args)

Invokes a server side hub method.

  • Returns a task that represents the completion of the method call on the server.

Example

myHub.Invoke("MethodOnTheServer", 10, new { Name = "SignalR" }).ContinueWith(task =>
{
     if(task.IsFaulted) 
     {
        Console.WriteLine("An error occurred during the method call {0}", task.Exception.GetBaseException());
     }
     else
     {
        Console.WriteLine("Successfully called MethodOnServer");
     }
});

Task<T> Invoke<T>(string action, params object[] args)

Invokes a server side hub method.

  • Returns a generic task with the return value of the hub.

Example

myHub.Invoke("GetValue").ContinueWith(task => 
{
    Console.WriteLine("Value from server {0}", task.Result);
});

object this[string name]

This indexer provides access to the state objects that can be set between server and client.

myHub["name"] = "Hello";

T GetValue<T>() (extension method)

Provides typed access to the state object on the hub.

var person = myHub.GetValue<Person>("person");
Console.WriteLine(person.Name);

Using Rx with SignalR.Client hub connections

To use Rx with hub events, call the Observe extension method on IHubProxy:

IObservable<object[]> observable = myHub.Observe("receive");
observable.Subscribe(args => Console.WriteLine(args[0]));

Clone this wiki locally