Utility to keep single SignalR connection for all browser windows (tabs) of the same application
Switch branches/tags
Nothing to show
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Initial commit Feb 1, 2015
README.md Added an example for script references (see #9) Nov 29, 2018
icon.png Icon added Feb 1, 2015
iwc-signalr.js Added method `getConnectionId` (#7) May 24, 2015
signalr-patch.js Fix typo in signalr-patch Apr 21, 2015

README.md

IWC-SignalR

Browsers have a limitation of the maximum number of connections to the same host address (same application). So, this limits the maximum number of opened browser windows of the same application with SignalR connections. IWC-SignalR allows to bypass this restriction using single SignalR connection for all windows of the same application. IWC-SignalR was tested with SignalR v2.2.0.

How it works

One of the windows becomes a connection owner (choosen randomly) and holds the real SignalR connection. If connection owner is closed or crashed another window becomes a connection owner - this happens automatically. Inter-window communication is done by means of IWC.

Usage

To use IWC-SignalR include signalr-patch.js and iwc-signalr.js files on your page. signalr-patch.js allows to start SignalR multiple times. This allows to reconfigure hub proxies at any time.

IWC-SignalR works with auto generated proxies. So, you should also include a reference <script src="signalr/hubs"></script> before signalr-patch.js and iwc-signalr.js. For example:

<!--Reference the SignalR library. -->
<script src=".../jquery.signalR-2.2.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>

<script src=".../iwc-all.js"></script>
<script src=".../signalr-patch.js"></script>
<script src=".../iwc-signalr.js"></script>

Let's have a hub Echo with method Send defined on server. Method Send calls method displayMsg of all clients.

var echoHub = SJ.iwc.SignalR.getHubProxy('echo', {
    client: {
        displayMsg: function (msg) {
          console.log(msg);
        }
    }
});
SJ.iwc.SignalR.start().done(function () {
    console.log('started');
    echoHub.server.send('test').done(function () {
        console.log('sent');
    });
});
//Result in console:
//started
//test
//sent

Dependencies

IWC-SignalR depends on IWC

API

Connection starting

SJ.iwc.SignalR.start()

Parameters and return value:

This function has the same signature as the original $.connection.hub.start function

Description:

SJ.iwc.SignalR.start when called it tries to find connection owner and connect to it via IWC. If connection owner not found, real SignalR connection is established and this window becomes a connection owner.

Obtaining hub proxy

{object}SJ.iwc.SignalR.getHubProxy({string}hubName, {object}proxyConfig)

Parameters:
  • hubName - hub name according to SignalR naming convention (starts from lower case letter)
  • proxyConfig - object which contains definition of client's methods
Return value:

Returns hub proxy object with server and client

Description:

Creates hub proxy object with server and client. Usage of server and client doesn't depend on whether the SignalR connection is real or via IWC.

Example:
var echoHub = SJ.iwc.SignalR.getHubProxy('echo', {
    client: {
        displayMsg: function (msg) {
          console.log(msg);
        }
    }
});

echoHub.server.send('test');

Check if conection owner

{bool}SJ.iwc.SignalR.isConnectionOwner()

Return value:

Returns true if this window is the owner of SignalR connection

Who is conection owner

{string}SJ.iwc.SignalR.getConnectionOwnerWindowId()

Return value:

Returns the ID of connection owner window. Returns null if connection owner not found.

Description:

This method works properly only if SJ.iwc.WindowMonitor is initialized.

Current state

SJ.iwc.SignalR.getState()

Return value:

Returns connection state (see SignalR's $.connection.connectionState for available values)

Description:

Returns the state of real SignalR connection

Connection Id

SJ.iwc.SignalR.getConnectionId()

Return value:

Returns connection id (see SignalR's $.connection.hub.id)

Description:

Returns the connection id of real SignalR connection

Events

Use SJ.iwc.SignalR.on() and SJ.iwc.SignalR.un() to subscribe/unsubscribe on events

  • statechanged(newState, prevState) - fired when connection state is changed. newState, prevState - new and previous state (see SignalR's $.connection.connectionState for available values)
  • connected - fired when state is changed to $.connection.connectionState.connected
  • starting, received, connectionslow, reconnecting, reconnected, disconnected - are the same events as for original SignalR connection
Example:
var someObject = {
    onStateChanged: function (newState, prevState) {
    }
};
//subscribe
SJ.iwc.SignalR.on('statechanged', someObject.onStateChanged, someObject);
//unsubscribe
SJ.iwc.SignalR.un('statechanged', someObject.onStateChanged, someObject);