Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR between initial load and updates

I'm trying to build an application with signalR. The application outline will be:

  • Load the initial state from the database
  • Subscribe to succeeding messages

I was looking for a set of best practices. If I subscribe to the updates after initial load there is a risk of missing updates. Between initial load and subscription there could be and update and that update I would miss.

Secondly I could use some sort of object state id and then first subscribe to the update stream, retrieve the data and then apply the updates if the objects are behind. This would require quite some code.

Are there any best practices to handle this?

like image 224
Patrick Avatar asked May 05 '15 20:05

Patrick


People also ask

How long do SignalR connections stay open?

The default keepalive timeout period is currently 20 seconds. If your client code tries to call a Hub method while SignalR is in reconnecting mode, SignalR will try to send the command. Most of the time, such attempts will fail, but in some circumstances they might succeed.

Is SignalR asynchronous?

SignalR is an asynchronous signaling library for ASP.NET that our team is working on to help build real-time multi-user web application.

What is long polling in SignalR?

Long polling is about doing over the Web the same things you do in a desktop scenario. With long polling, the client places the request and the server doesn't reply until it has information to return. The Web client keeps a pending connection that's closed only when some valid response can be returned.

How does SignalR hub work?

What is a SignalR hub. The SignalR Hubs API enables you to call methods on connected clients from the server. In the server code, you define methods that are called by client. In the client code, you define methods that are called from the server.


1 Answers

There could be a miss depending on when you query for updates. For instance, say your backend queries for some notifications and pushes this down to the client, then the user connects (subscribes) for notifications - the original data set you pushed could be stale if an update was sent during this init phase and missed.

What I have found, as a best practice, is to scrap sending any initial data. Instead, subscribe as soon as you can and query for updates on a successful connect. for example....

var proxy = $.connection.yourHub;

$.connection.hub.start()
    .done(function () { // <--- immediately call once connected

        // define and call server hub method
        // this will query updates and immediately
        // invoke the associated client function

        proxy.server.queryUpdates();
    })
    .fail(function () {
        console.log('Could not connect!');
    });

proxy.client.serveUpdates = function(updates) {
    // here's your fetched updates on initial connect
}

// YourHub.cs
public void QueryUpdates()
{
    var updates = this.yourService.yourQueryUpdatesmethod();

    Clients.Caller.serveUpdates(updates) // this calls proxy.client.serveUpdates
}

This gives you the benefits of establishing your connection, listening for updates, and effectively serving the most fresh set of data in a one-time fashion when you connect.

Note this doesn't have to be a SignalR specific implementation. You could make a typical ajax call in this .done() callback to retrieve your results, but I don't see why you can't re-use all the logic you already have on your hub. You can essentially just craft this to call your existing hub method by explicitly invoking it from the client.

like image 61
scniro Avatar answered Sep 29 '22 08:09

scniro