Skip to content

Instantly share code, notes, and snippets.

@DamianEdwards
Created July 31, 2013 23:51
Show Gist options
  • Save DamianEdwards/6127298 to your computer and use it in GitHub Desktop.
Save DamianEdwards/6127298 to your computer and use it in GitHub Desktop.
Hub methods with progress support
$.connection.hub.start().done(function() {
var hub = $.connection.myHub;
hub.server.doLongRunningThing()
.progress(function(p) {
console.log("Long running thing: " + p + "%");
})
.done(function() {
console.log("Long running thing complete!");
})
.fail(function(e) {
console.log("Long running thing failed: " + e.message);
});
});
public class MyHub : Hub
{
// The hub method must be Task returning and accept an IProgress<T>
public async Task DoLongRunningThing(IProgress<int> progress)
{
for (var i = 0; i <= 100; i+=5)
{
await Task.Delay(200);
progress.Report(i);
}
}
}
@drub0y
Copy link

drub0y commented Aug 1, 2013

Love the idea of this being a first class concept and the proposed API design seems nice.

Should the IProgress::Report method itself be async or...? I realize it's just queuing up a message to eventually broadcast out, but is there any async opportunity under that?

Also, how does the progress reporting work? Is it 1:1 with the caller or does everyone listening for progress get the notification despite who calls it?

@DamianEdwards
Copy link
Author

@drub0y the progress reporting is 1:1 to the method caller. It's supposed to represent progress of the method duration, so you can't report progress after the method has returned (it throws). This is now implemented in a feature branch (https://github.com/signalr/signalr/tree/feature-2380-hub-progress) if you're interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment