Persistent Connection in SignalR

In this post we will learn about SignalR persistent connection class and their frequently used methods.

“PersistentConnection” is low level abstract class which gives more control on the SignalR Connections that the hub classes. Below are some frequently used virtual methods which called when a certain event occurs during the SignalR Connection life cycle. The methods are defined as virtual so you can override it and add your own implement for it.

1. protected virtual Task OnConnected(IRequest request, string connectionId);  — “OnConnected” method fired when a new connection is established . Here request is special object which provide information about connected user, cookies etc. “connectionId” is a GUID value.

2. protected virtual Task OnDisconnected(IRequest request, string connectionId, bool stopCalled)  — “OnDisconnected” method fired when client get disconnected from the server . Here stopCalled is boolean value, it true when connection is closed happily by the client and false when client get disconnected due to some connectivity issue for example network problem ,connection timeout etc.

3. protected virtual Task OnReceived(IRequest request, string connectionId, string data)–  “OnReceived” method get fired when client send some data and it is received on the server. Here data is type of string which frees client to send any kind of data which can be serialized/de-serialized  as a string.

4. protected virtual Task OnReconnected(IRequest request, string connectionId); – “OnReconnected” method get fired when connection re-established after a timeout.

Here one thing you can notice that the above method return type is Task  Type object which notify that requests  can be processed in async manner.

5. protected virtual IList OnRejoiningGroups(IRequest request, IList groups, string connectionId) – “OnRejoiningGroups” method get fired when lost connection is re-established and determine in which groups the current connection should be reassigned .

6. protected virtual bool AuthorizeRequest(IRequest request) – “AuthorizeRequest” method get fired for every request . it gives the capability to  check and handle authorization / un-authorization of request made by client.

Below are a sample class which have override method for above methods.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRPersistent
    public class ChatServer:PersistentConnection
        protected override System.Threading.Tasks.Task OnConnected(IRequest request, string connectionId)
            return base.OnConnected(request, connectionId);
        protected override System.Threading.Tasks.Task OnDisconnected(IRequest request, string connectionId, bool stopCalled)
            return base.OnDisconnected(request, connectionId, stopCalled);
        protected override System.Threading.Tasks.Task OnReconnected(IRequest request, string connectionId)
            return base.OnReconnected(request, connectionId);
        protected override System.Threading.Tasks.Task OnReceived(IRequest request, string connectionId, string data)
            return base.OnReceived(request, connectionId, data);
        protected override IList<string> OnRejoiningGroups(IRequest request, IList<string> groups, string connectionId)
            return base.OnRejoiningGroups(request, groups, connectionId);
        protected override bool AuthorizeRequest(IRequest request)
            return base.AuthorizeRequest(request);

.csharpcode, .csharpcode pre
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
background-color: #f4f4f4;
width: 100%;
margin: 0em;
.csharpcode .lnum { color: #606060; }


In the next post we will learn how to start work which signalr client using Persistent connections. Hope you find this post useful , Thanks for reading.