Friday, December 14, 2012

SignalR Performance Counters

One of the hidden gems in the SignalR framework is a utility called signalr.exe (in the Microsoft.AspNet.SignalR.Hosting.Utils package) that you can use to do a few things.

  • Install/Uninstall SignalR performance counters
  • Generate a static js file for the magic ~/signalr/hubs url.

After installing the Microsoft.AspNet.SignalR.Hosting.Utils NuGet package, you’ll find a SignalR executable in the package folder(under \packages\Microsoft.AspNet.SignalR.Hosting.Utils.1.0.0-alpha2\tools)

  • Open a command prompt(run as Administrator).
  • Browse to the tools folder containing the SignalR.exe file.
  • Run signalr ipc to install the Performance Counters. These performance counters give you more detailed information about your SignalR applications.

clip_image002

    • Remark: To use these on full IIS you need to add the app pool user for your application to the Performance Monitor Users group.
  • Run signalr ghp /path:<path to binaries> to generate a static JavaScript file containing the hub code. This file(server.js) is generated inside the same tools folder.

image

  • If we open up the file we see the magic that SignalR creates for us when calling the ~/signalr/hubs url:
/*!
* ASP.NET SignalR JavaScript Library v1.0.0
* http://signalr.net/
*
* Copyright Microsoft Open Technologies, Inc. All rights reserved.
* Licensed under the Apache 2.0
* https://github.com/SignalR/SignalR/blob/master/LICENSE.md
*
*/

/// <reference path="..\..\SignalR.Client.JS\Scripts\jquery-1.6.2.js" />
/// <reference path="jquery.signalR.js" />
(function ($, window) {
/// <param name="$" type="jQuery" />
"use strict";

if (typeof ($.signalR) !== "function") {
throw new Error("SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/hubs.");
}

var signalR = $.signalR;

function makeProxyCallback(hub, callback) {
return function () {
// Call the client hub method
callback.apply(hub, $.makeArray(arguments));
};
}

function registerHubProxies(instance, shouldSubscribe) {
var key, hub, memberKey, memberValue, subscriptionMethod;

for (key in instance) {
if (instance.hasOwnProperty(key)) {
hub = instance[key];

if (!(hub.hubName)) {
// Not a client hub
continue;
}

if (shouldSubscribe) {
// We want to subscribe to the hub events
subscriptionMethod = hub.on;
}
else {
// We want to unsubscribe from the hub events
subscriptionMethod = hub.off;
}

// Loop through all members on the hub and find client hub functions to subscribe/unsubscribe
for (memberKey in hub.client) {
if (hub.client.hasOwnProperty(memberKey)) {
memberValue = hub.client[memberKey];

if (!$.isFunction(memberValue)) {
// Not a client hub function
continue;
}

subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue));
}
}
}
}
}

signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false })
.starting(function () {
// Register the hub proxies as subscribed
// (instance, shouldSubscribe)
registerHubProxies(signalR, true);

this._registerSubscribedHubs();
}).disconnected(function () {
// Unsubscribe all hub proxies when we "disconnect". This is to ensure that we do not re-add functional call backs.
// (instance, shouldSubscribe)
registerHubProxies(signalR, false);
});

signalR.testHub = signalR.hub.createHubProxy('testHub');
signalR.testHub.client = { };
signalR.testHub.server = {
test: function () {
/// <summary>Calls the Test method on the server-side TestHub hub.&#10;Returns a jQuery.Deferred() promise.</summary>
return signalR.testHub.invoke.apply(signalR.testHub, $.merge(["Test"], $.makeArray(arguments)));
}
};

}(window.jQuery, window));

No comments: