Skip to content

SignalR Client Hubs

davidfowl edited this page Jan 6, 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 handler. But will instead point to the root of your site.

Example

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

IHubProxy CreateProxy(string hubName)

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

  • Returns an IHubProxy.
  • NOTE: The name of this hub needs to be the full type name of the hub.

Example

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

Task Start()

Starts the connection for all hubs.

  • NOTE: Make sure to subscribe for events before calling start.

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.

Example

Subscription sub = myHub.Subscribe("receive");
sub.Data += args =>
{
    Console.WriteLine(args[0]);
};

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.

Subscription 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!"));

Subscription 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));

Subscription 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);

Subscription API

A subscription can be used to add or remove event handlers for a specific hub event. Create new subscriptions with hubProxy.Subscribe.

Data (event Action<object[]>)

Triggered when data is available for the Subscription.

  • The arguments in the event handers represent arguments from the server side triggering of the event.
  • The data at each argument index will be a JSON object.

Example

subscription.Data += args => 
{
     dynamic person = args[0];
     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