Skip to content

Utility to keep single SignalR connection for all browser windows (tabs) of the same application

License

Notifications You must be signed in to change notification settings

slimjack/IWC-SignalR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);

About

Utility to keep single SignalR connection for all browser windows (tabs) of the same application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published