Skip to content

SignalR Client Hubs

davidfowl edited this page Nov 5, 2011 · 26 revisions

SignalR 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 HubConnection.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 HubConnection.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 IHubProxy.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 a bunch of 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 a bunch of 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);
});

Subscription API

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

Subscription.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