Skip to content

PersistentConnection

mitcht edited this page Apr 25, 2012 · 38 revisions

Persistent Connections (Server)

A PersistentConnection is the base class that has an api for exposing a SignalR service over http.

To create a new endpoint. Create a new class that derives from PersistentConnection.

public class MyEndPoint : PersistentConnection 
{
}

To make this endpoint accessible, add a route to Application_Start (or something similar):

using SignalR.Hosting.AspNet.Routing;

public void Application_Start() {
    RouteTable.Routes.MapConnection<MyEndPoint>("echo", "echo/{*operation}");
}

This will expose /echo as a SignalR endpoint.

PersistentConnection API

Task OnConnectedAsync(IRequest request, IEnumerable groups, string clientId)

Called when a new connection is made.

  • request - The request associated with this connection.
  • groups - A list of groups the client is a part of.
  • clientId - The client id of the new connection.

Example

public class MyEndPoint : PersistentConnection 
{
    protected override Task OnConnectedAsync(IRequest request, IEnumerable<string> groups, string clientId)
    {
        return Connection.Broadcast("Client " + clientId + " connected");
    }
}

Task OnReconnectedAsync(IRequest request, IEnumerable groups, string clientId)

Called when a connection is restored.

  • request - The request associated with this connection.
  • groups - A list of groups the client is a part of.
  • clientId - The client id of the new connection.

Example

public class MyEndPoint : PersistentConnection 
{
    protected override Task OnReconnectedAsync(IRequest request, IEnumerable<string> groups, string clientId)
    {
        return Connection.Broadcast("Client " + clientId + " re-connected");
    }
}

Task OnReceivedAsync(string clientId, string data)

Called when data is received on the connection.

  • clientId - The id of the client that sent the data
  • data - The payload received from the client

Example

public class MyEndPoint : PersistentConnection 
{
    protected override Task OnReceivedAsync(string clientId, string data) 
    {
        return Connection.Broadcast("Client " + clientId + " sent " + data);   
    }
}

Task OnDisconnectAsync(string clientId)

Called when a client is disconnected from the connection.

  • clientId - The id of the client that disconnected

Example

public class MyEndPoint : PersistentConnection 
{
    protected override Task OnDisconnectAsync(string clientId) 
    {
        return Connection.Broadcast("Client " + clientId + " disconncted");   
    }
}

Task OnErrorAsync(string clientId)

Called when an error occurs on the connection.

  • clientId - The id of the client the error occured on
  • NOTE: You can use OnError if you just want to do synchronous work and not have to return a Task.

Example

public class MyEndPoint : PersistentConnection 
{
    protected override Task OnErrorAsync(string clientId) 
    {
        return Connection.Broadcast("Error with " + clientId);   
    }
}

Task Send(string clientId, object value)

Sends a message to the specified client.

  • clientId - Client id to send the message to
  • data - The payload to send
  • NOTE: Objects will be JSON serialized

Example

public class MyEndPoint : PersistentConnection 
{
    protected override Task OnReceivedAsync(string clientId, string data) 
    {
        return Send(clientId, data);   
    }
}

Task AddToGroup(string clientId, string groupName)

Adds a client to the specified group.

  • clientId - client id to add to the group
  • groupName - name of the group to add the client to
  • NOTE: The task returned from this method does not mean the client was successfully added to the group.

Example

public class MyEndPoint : PersistentConnection 
{
    protected override Task OnReceivedAsync(string clientId, string data) 
    {
        return AddToGroup(clientId, "foo");   
    }
}

Task RemoveFromGroup(string clientId, string groupName)

Removes a client from the specified group.

  • clientId - client id to remove from the group
  • groupName - name of the group to remove the client from
  • NOTE: The task returned from this method does not mean the client was successfully removed from the group.

Example

public class MyEndPoint : PersistentConnection 
{
    protected override Task OnReceivedAsync(string clientId, string data) 
    {
        return RemoveFromGroup(clientId, "foo");   
    }
}

Task SendToGroup(string groupName, object value)

Sends a message to the specified group.

  • groupName - name of the group to send the message to
  • value - The payload to send
  • NOTE: Objects will be JSON serialized

Example

public class MyEndPoint : PersistentConnection 
{
    protected override Task OnReceivedAsync(string clientId, string data) 
    {
        return SendToGroup("foo", data);   
    }
}

IConnection Connection

  • Gets the IConnection for this PersistentConnection.

IConnection API

Task Broadcast(object value)

Broadcasts a message to all on this connection.

  • NOTE: the value is JSON serialized

Broadcasting over a connection from outside of a PersistentConnection

Sometimes you have some arbitrary code in an application that you want to be able to notify all clients connected when some event occurs. An example of this might be a background task that sends data at a certain interval.

You can use the dependency resolver for the current host to resolve the IConnectionManager interface which allows you to get ahold of the connection object.

In these scenarios, you can get an IConnection for a specific persistent connection by calling connectionManager.GetConnection<T>. In ASP.NET you can do:

using SignalR.Infrastructure;

IConnectionManager connectonManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
IConnection connection = connectonManager.GetConnection<MyEndPoint>();

Once you have the connection, you can call Broadcast to send a message to all clients.

For a self-host (console application) you can access the DependencyResolver on the server instance. Make sure you include 'using SignalR.Infrastructure;' otherwise the Resolve method might not show up (it's an extension method).

Clone this wiki locally