HotTowel (Durandal really) and SignalR initialisation
Asked Answered
G

1

8

So I'm integrating SignalR and HotTowel, although really I think this is a matter of how to integrate with Durandal itself.

The issue is I have obviously multiple views. Some of these views I want to respond to SignalR messages. The question is how to do this integration considering that SignalR events have to be started before I call SignalR's hub start method.

So take the example I have view1 and view2. I want each to do something when a SignalR message is received and in the context of that view (so let's say update the DOM somehow). It's an SPA obviously so calling the SignalR start method for each view seems like a bad idea, so starting SignalR once at boot sounds like the right plan, but at that point my views may not have been loaded, and still how would I ensure that my events have the right context for the page.

This is based on my understanding that all events for SignalR have to be registered before I call start. Any thoughts clever people of StackOverflow?

Edit to expand on the problem

Part of the website involves uploading files for parsing and processing to import into a database. I have created a view where the file is selected and uploaded (using FineUploader) to a WebApiController. The controller does the basic steps of checking the uploaded file and then starts an async task to actually do the parsing and processing, while immediately returning the basic "Yep that uploaded fine" message.

This causes the list of 'in progress' files to refresh and the file appears with an 'Uploaded' status. As the async task occurs, the file is parsed, then processed against a rules system, and then finally imported into another back end data store. As each of these status changes occur, SignalR sends messages to the client to notify them of these changes, and thus update the status against the filename. In order for this to occur I must attach a function to the event as it received in SignalR. That even needs some kind of reference to my view (actually viewmodel) so it can update the correct value.

As SignalR should be started once with a call to hub.Start(), I am trying to do it during the 'boot' phase. However when my SPA starts, that view has not been loaded, and therefore neither has that viewmodel, and therefore my function that is responsible for initialising SignalR can have no understanding of the view/viewmodel it must update.

Examples I've seen on using SignalR show it being used in one view, but that doesn't really work surely if you need it in multiple views (you can't just keep calling hub.start() can you)?

Sorry, if this still doesn't make sense I'll post some code or something.

Giorgia answered 9/6, 2013 at 20:43 Comment(1)
can you describe the problem your trying to solve some more? And what have you tried?Putt
L
9

If you use

$.connection.myHub.on("myMethod", function (/* ... */) { /* ... */ });

instead of

$.connection.myHub.client.myMethod = function (/* ... */) { /* ... */ };

you can add client-side hub methods after calling $.connection.hub.start();

Licketysplit answered 13/6, 2013 at 2:57 Comment(5)
Excellent! exactly the technique I was looking for. I google'd things like 'signalR event after hub start' and some reason just couldn't find anything describing this alternative! Thanks halter73!Giorgia
Not just Durandal or HotTowel, but any SPA application benefits from this. I'm working on a Backbone, Underscore app and this resolves my issues with SignalR not receiving push messages.Coz
I will add that you should have at least one callback per hub wired before you start the connection. It doesn't matter if the callback no-ops. Once you do that, you should be able to add an remove callbacks as you please, even while the SignalR connection is open.Licketysplit
@Licketysplit Thanks, having one callback per hub wired before you start the connection solved my particular problem. Do you have a reference for this?Nashville
@tjrobinson Take a look at the "Note" in the "How to establish a connection" section of the following guide: asp.net/signalr/overview/signalr-20/hubs-api/…Licketysplit

© 2022 - 2024 — McMap. All rights reserved.