Tag Archives: signalR

[new ideas] using signalR to support long time consuming operations on server

In many situations i faced with problem when i have to make an operation which will be done on server side but this operation is very time consuming. For example reading structure for 100 parts from our ERP system. If i want to do it in one call from my web application then user starts process and for long time (e.g. 10 minutes) is not notified about what is really happening on server. In such situation user can start to think that something wrong happen and tries to make a call again for example.

Expected situation for me in that case would be to give user feedback that something is still going on server. Then user would know. Ok, it is time consuming process but still going well so i need to be patient.

Until today it was only a dream for me and i had no idea how to develop something like this.

But as you can find from this post i start to play a little with signalR. And today i made an experiment to see if signalR can help to solve this problem?

So i did an experiment and i know now that answer for this question is yes.

Here is how i did an experiment. You can implement your own solution as you want.

Setup on server side:

Web application with included signalR

Setup on client side:

Javascript application with jquery and knockout model in it.

Implementation:

On server i have hub named tester with following code:

Imports Microsoft.AspNet.SignalR 

Public Class tester
Inherits Hub

Public Sub hallo(id As String)
Clients.All.start("New client has loggin with guid: " + id)
End Sub

Public Sub test()
Clients.Caller.start("Starting at: " + Now.ToString)

For i = 0 To 30
Clients.Caller.step("Step: " + (i + 1).ToString + " " + Now.ToString)
System.Threading.Thread.Sleep(20000)
Next

Clients.Caller.koniec("Finish at: " + Now.ToString)
End Sub

End Class

My hub has two methods:

1. hallo – which get’s one attribute id – it is connection id and this methods send’s information for all connected clients that new user has been connected. it is only infomration method in our example.

2. test – this is right test method. At starts – sends information for client that process has been started, next makes fake long operation which is loop for 31 iteration. In loop it send’s information to client with step number of loop and freezes thread for 20 seconds. At the end sends information that process is finished. That means all process takes over 10 minutes. Which is enough long to make usual timeout.

Method test calls 3 different method on client side – start, step and finish

In client we have following application deffinition in Javascript.

function model() {
var m = this;
m.info = ko.observableArray();

m.startProcess = function () {
m.info.push(new info("Sending start call"));
myHub.server.test();
m.info.push(new info("Start call done"));
};
};

function info(i) {
var m = this;
m.Text = ko.observable(i);
}

var myHub;

$(function () {

myHub = $.connection.tester;
myHub.client.start = function (o) {
myModel.info.push(new info(o));
};
myHub.client.step = function (o) {
myModel.info.push(new info(o));
};
myHub.client.koniec = function (o) {
myModel.info.push(new info(o));
};

var myModel = new model();
ko.applyBindings(myModel);
$.connection.hub.start().done(function () { myHub.server.hallo($.connection.hub.id);});

});

First we have knockout model with observable array named info and one method which send call of tester on server.

Next we have knockout object of info which just presents data from server.

Finnaly we have start definition:

First step is definition of hub object tester from server. Next is definition of handling events send from server for start, finish and step methods. Next is definition of knockoout model and applying bindings to web page. Finally start of connection and after connection is succeeded  then calling method hallo on server.

on page we have button which calls method startProcess and unsorted list which presents contains of info array. When user clicks button starting process then process on server is starting and server notifies each 20 seconds loop that something i still going on server and finally sends finish information.

Here is how it looks in browser when process will finish:

signalr1