Skip to content
This repository

SignalR .NET Client (Persistent Connections)

The SignalR client works with .NET applications as well as WindowsPhone applications.

Setup

See the Getting Started page to find the appropriate package.

This documentation explains how to interact with a PersistentConnection, to see the documentation on interacting with Hubs see SignalR Client Hubs.

Connection API

To connect to a PersistentConnection endpoint, create a connection with the appropriate URL:

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

Task Start()

Starts the connection.

  • Returns a task that represents when negotiation is complete.

Example

connection.Start().ContinueWith(task => 
{
    if(task.IsFaulted) 
    {
        Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
    }
    else 
    {
        Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
        // Do more stuff here
    }
});

Task Send(string data)

Sends data over the connection.

  • Returns a task that represents when the send operation is complete.

Example

connection.Send("Hello").ContinueWith(task =>
{
    if(task.IsFaulted)
    {
        Console.WriteLine("Send failed {0}", task.Exception.GetBaseException());
    }
    else
    {
        Console.WriteLine("Success");
    }
});

void Stop()

Stops the current connection.

  • Raises the connection.Closed event.
  • Sets IsActive to false.

Example

connection.Stop();

event Action<string> Received

Triggered when data is received from the connection.

  • The string will be in JSON format.

Example

connection.Received += data => 
{
    Console.WriteLine(data);
};

event Action Reconnected

Triggered when the underlying connection is re-established.

Example

connection.Reconnected += ex =>
{
    Console.WriteLine("The connection was re-established");
};

event Action<Exception> Error

Triggered when there is an error.

  • NOTE: The connection is still active even after errors occur. To stop the connection, use conncection.Stop().
  • NOTE: After an error, the long polling transport will wait 2 seconds before trying to reconnect to the server.

Example

connection.Error += ex =>
{
    Console.WriteLine("An error occurred {0}", ex.Message);
};

event Action Closed

Triggered when the connection is closed. Calling stop will trigger this event.

connection.Closed += () =>
{
    Console.WriteLine("Connection with client id {0} closed", connection.ConnectionId);
};

bool IsActive

  • Gets a boolean value indicating if connection is still active.

string ClientId

  • Gets or sets the client Id for the current connection.

string MessageId

  • Gets or sets the message id for the current connection.

Using Rx with SignalR.Client connections

We also have an IObserable implementable for the connection object. To use it just call the AsObservable extension method.

  • Note, to use the Rx Linq extension methods you need to install the NuGet package for Reactive Extensions

Example

    IObservble<string> observable = connection.AsObservable();
    observable.Subscribe(data => Console.WriteLine(data));
Something went wrong with that request. Please try again.