Reputation: 34830
I have a SignalR hub where it is pushing some number increments (and decrements) to Javascript clients in a webpage. ASP.NET MVC page pulls the initial value from the database initially, and then SignalR kicks in. The methods are generally invoked, but some get lost in the way. For example:
x
is, say, 100000.x
is incremented at the client about 50-60 times, so say x
becomes 100055.x
is pulled from the database, and it's actual value of 100100.The hub code is extremely simple, and I'm logging when data comes from the server:
$(function () {
var connection = $.connection.adminhub;
var myToken = "@ViewBag.Code";
connection.client.increment = function (data) {
console.log("SignalR: increment " + data);
var num = parseInt($("#" + data).text());
$("#" + data).text(num + 1);
};
connection.client.decrement = function (data) {
console.log("SignalR: decrement " + data);
var num = parseInt($("#" + data).text());
$("#" + data).text(num - 1);
};
console.log("Connecting to SignalR...");
$.connection.hub.start().done(function () {
console.log("Connected to SignalR.");
connection.server.registerForIndex(myToken);
});
});
ViewBag.Code
is a special token written by the server, it works (otherwise it can't receive any data). There is only one place on the server which calls the hub's method:
static IHubContext Instance{
get
{
return GlobalHost.ConnectionManager.GetHubContext<AdminHub>();
}
}
public static void Increment(string key)
{
Instance.Clients.Group("stats").increment(key);
}
My client(s) is/are registered in the stats
group. I've tried with different browsers and different OS's, they act all the same. I've checked the lifetime events and there are no connection slowdowns, disconnects, reconnects. I am connected using websockets transport, and my connection to both the Internet and the server is quite stable. Why are my values off by about 50% and how can I correct this behavior?
Upvotes: 2
Views: 3221
Reputation: 950
Keep in mind that retrieving data from signalr is an asynchronous operation. Even though Javascript is single threaded you cant be sure that a picked number is incremented before another request picked the same number from the html element. Maybe extend your log method to see what numbers are actually picked from the HTML Element.
However, 100 increments per minute should be feasable. I recommend to subscribe to the lifetime events and have a look at the connection status. Maybe the connection is slow or needs to reconnect quite often.
http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client#connectionlifetime
Upvotes: 1