Log in

SignalR and Realtime

From MDrivenWiki

What is SignalR and what is it used for

SignalR is a .net software componenent used for Realtime communication. By realtime we mean that 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 communcations 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 what you could before.
  2. MDrivenTurnkey - javascript client polled the server frequently to see if there where 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 - buut 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 change 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 Mdr server "RealtimeChangeInClient" with id of object and model-number of the feature. The MDr-Server reacts with 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 was currently current it is set to invalidated and the InvalidateByRealtimeDone event is fired on the IPersistenceService implementation. Turnkey EcoSpaces has implemented this event (InvalidateByRealtimeDone) with a call to SignalLiveClientsWeGotNews that in turn calls all turnkeyjavascript clilents with AskClientToPullNowWeGotNews (signalR).Live clients will ask their TK-server for an update - if the client shows and UI that containts this realtime attribute it will be accessed and we will discover that it is in invalidated state - we then fetch it from db and deliver to client as normal refresh loop.

Updates 2021 Feb

SignalR actually comes in 2 versions; one called SignalR and one 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 in WPF Prototyper in MDrivenDesigner(.net Framework) - we have now added the capability to connect to a 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 make use of the RealtimeStateSubscriberService will first try and connect to MDrivenServer Framework version with SignalR - if it fails it will then try and connect with SignalRCore.

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

Verify SignalR can connect (.net Framework)

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