22

Strange behavior is happening when using signalR with IE 11. Scenario:

We have some dispatcher type functionality where the dispatcher does some actions, and the other user can see updates live (querying). The parameters that are sent come through fine and cause updates on the IE client side without having to open the developer console.

BUT the one method that does not work (performUpdate - to get the query results - this is a server > client call, not client > server > client) - never gets called. IT ONLY GETS CALLED WHEN THE DEVELOPER CONSOLE IS OPEN.

Here's what I've tried:

Why JavaScript only works after opening developer tools in IE once?

SignalR : Under IE9, messages can't be received by client until I hit F12 !!!!

SignalR client doesn't work inside AngularJs controller

Some code snippets

Dispatcher side

On dropdown change, we get the currently selected values and send updates across the wire. (This works fine).

$('#Selector').on('change', function(){
   var variable = $('#SomeField').val();
   ...
   liveBatchHub.server.updateParameters(variable, ....);
});

Server Side

When the dispatcher searches, we have some server side code that sends out notifications that a search has been ran, and to tell the client to pull results.

public void Update(string userId, Guid bId)
        {
            var context = GlobalHost.ConnectionManager.GetHubContext<LiveBatchViewHub>();
            context.Clients.User(userId).performUpdate(bId);
        }

Client side (viewer of live updates)

This never gets called unless developer tools is open

liveBatchHub.client.performUpdate = function (id) {
                    //perform update here
                    update(id);
                };

Edit

A little more information which might be useful (I am not sure why it makes a difference) but this ONLY seems to happen when I am doing server > client calls. When the dispatcher is changing the search parameters, the update is client > server > client or dispatcher-client > server > viewer-client, which seems to work. After they click search, a service in the search pipeline calls the performUpdate server side (server > viewer-client). Not sure if this matters?

Edit 2 & Final Solution

Eyes bloodshot, I realize I left out one key part to this question: we are using angular as well on this page. Guess I've been staring at it too long and left this out - sorry. I awarded JDupont the answer because he was on the right track: caching. But not jQuery's ajax caching, angulars $http.

Just so no one else has to spend days / nights banging their heads against the desk, the final solution was to disable caching on ajax calls using angulars $http.

Taken from here:

myModule.config(['$httpProvider', function($httpProvider) {
    //initialize get if not there
    if (!$httpProvider.defaults.headers.get) {
        $httpProvider.defaults.headers.get = {};    
    }    

    // Answer edited to include suggestions from comments
    // because previous version of code introduced browser-related errors

    //disable IE ajax request caching
    $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
    // extra
    $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
    $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
}]);
Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75

2 Answers2

12

I have experienced similar behavior in IE in the past. I may know of a solution to your problem.

IE caches some ajax requests by default. You may want to try turning this off globally. Check this out: How to prevent IE from caching Ajax with jQuery

Basically you would globally switch this off like this:

$.ajaxSetup({ cache: false });

or for a specific ajax request like this:

$.ajax({
  cache: false,
  //other options...
});

I had a similar issue with my GET requests caching. My update function would only fire off once unless dev tools was open. When it was open, no caching would occur.

Community
  • 1
  • 1
JDupont
  • 1,352
  • 10
  • 14
9

If your code works properly with other browsers, So the problem can be from the SignalR's used transport method. They can be WebSocket, Server Sent Events, Forever Frame and Long Polling based on browser support.

The Forever Frame is for Internet Explorer only. You can see the Introduction to SignalR to know which transport method will be used in various cases (Note that you can't use any of them on each browser, for example, IE doesn't support Server Sent Events).

You can understand the transport method being used Inside a Hub just by looking at the request's QueryString which can be useful for logging:

Context.QueryString["transport"];

I think the issue comes from using Forever Frame by IE likely, since sometimes it causes SignalR to crash on Ajax calls. You can try to remove Forever Frame support in SignalR and force to use the remaining supported methods by the browser with the following code in client side:

$.connection.hub.start({ transport: ['webSockets', 'serverSentEvents', 'longPolling'] });

I showed some realities about SignalR and gave you some logging/trace tools to solve your problem. For more help, put additional details :)

Update: Since your problem seems to be very strange and I've not enough vision around your code, So I propose you some instructions based on my experience wish to be useful:

  1. Setup Browser Link in IDE suitable
  2. checkout the Network tab request/response data during its process
  3. Make sure you haven't used reserved names in your server/client side (perhaps by renaming methods and variables)

Also I think that you need to use liveBatchHub.server.update(variable, ....); instead of liveBatchHub.server.updateParameters(variable, ....); in Dispatcher side to make server call since you should use server method name after server.

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70