SignalR and Realtime

What is SignalR and what is it used for?

SignalR is a .net software component used for Realtime communication. By Realtime, we mean communication done point to point at the time of need.

How is Realtime communication used in MDriven?

We use the newly added Realtime functionality in several ways.

  1. You can tag an attribute with "Realtime=true", and systems that use WebApi communications with an MDrivenServer will have the Attribute invalidated in a very short time after it was committed to the database. This functionality enables you to discover changes and react to them faster with less resource consumption than you could before.
  2. MDrivenTurnkey - the Javascript client polled the server frequently to see if there were any updates to render. Now, the client instead waits for an event from the server "WeGotNews" and then polls. Currently, the client will still poll without this event - but at a much lower frequency (68s instead of 8s).
  3. WPF - Wecpof applications did not have the auto polling as Turnkey used; instead, a refresh poll was done on ViewOpen. This has now changed and WPF-Wecpof also has the auto refresh. To avoid needing to do the refresh when nothing has changed, we have an "AllIsWell" event sent from the MDrivenServer that also gives information on the sync-version-id - that the client can use to decide if a poll is needed or not.
Signaling for Realtime

When a member changes PersistenceState from dirty to current - as it does on save complete - it checks if it was tagged with Realtime TV. If so, it ends up in Cache_OnRealtimeChangeDetected. From here, we signal the MDriven Server "RealtimeChangeInClient" with the ID of the object and the model number of the feature. The MDriven Server reacts by sending a RealtimeStateUpdate event over signalR to all clients(clients are typical ecospaces in TurnkeyServers).

Clients check if the object is loaded and if so, call InvalidateIfNotAlready on the specific member that Async will check the current state of the member. If it is current, it is set to invalidated and the InvalidateByRealtimeDone event is fired on the IPersistenceService implementation.

Turnkey EcoSpaces have implemented this event (InvalidateByRealtimeDone) with a call to SignalLiveClientsWeGotNews that in turn calls all Turnkey-Javascript clients with AskClientToPullNowWeGotNews (SignalR). Live clients will ask their TK server for an update - if the client shows and UI that contains this Realtime attribute, it will be accessed and we will discover that it is in an invalidated state. We then fetch it from the DB and deliver it to the client as a normal refresh loop.

Updates 2021 February

SignalR actually comes in 2 versions: SignalR and SignalRCore.

  • The 2 versions are not message compatible.
  • The 2 versions can act as a client in both .net core and .net Framework(>4.6.1).

As we have the MDrivenServer- core version and the built-in WPF Prototyper in MDrivenDesigner(.net Framework), we have now added the capability to connect to an MDrivenServer-Core version from the MDrivenDesigner.

If you have Framework-based clients, you can also do so by adding the Nuget package Microsoft.AspNetCore.SignalR.Client.

A Framework-based client that makes use of the RealtimeStateSubscriberService will first try to connect to an MDriven Server Framework version with SignalR. If it fails, it will then try to connect with SignalRCore.

The Turnkey Javascript client will also first try Framework communication and if it fails to connect, will switch to core communication.

Load balancing with SignalR

Please note that the .Net Framework version as implemented in Turnkey is not compatible with cookie based load balancing.

Verify SignalR can connect (.net Framework)

If you are behind a reverse proxy and you have trouble getting SignalR to connect, then try these calls in your browser and make sure they are not blocked:

This page was edited 28 days ago on 06/17/2024. What links here