feat: enable subscription logic for messengers

In order to help control traffic over the websocket, a subscription feature has been added:
* A config option, `enableMessengerSubscriptions` has been added
* When true, the MessengerBase class will assume that any message sent using the `PostStatusMessage` that has a valid client ID wants to send any subsequent unsolicited updates to that same client
* The client's ID will be added a list of subscribed client IDs
* Any subsequent messages sent using the `PostStatusMessage` methods that have a null clientId will ONLY be sent to subscribed clients
* When a client disconnects, it will be removed from the list of subscribed clients

This should cut down drastically on the traffic to the UI, especially when combined with requesting partial status updates from a device rather than the entire state.
This commit is contained in:
Andrew Welker
2025-09-26 21:31:54 -05:00
parent 087d0a1149
commit bb694b4200
6 changed files with 239 additions and 9 deletions

View File

@@ -54,7 +54,10 @@ namespace PepperDash.Essentials
StringComparer.InvariantCultureIgnoreCase
);
public Dictionary<string, List<IMobileControlAction>> ActionDictionary => _actionDictionary;
/// <summary>
/// Actions
/// </summary>
public ReadOnlyDictionary<string, List<IMobileControlAction>> ActionDictionary => new ReadOnlyDictionary<string, List<IMobileControlAction>>(_actionDictionary);
private readonly GenericQueue _receiveQueue;
private readonly List<MobileControlBridgeBase> _roomBridges =
@@ -66,6 +69,16 @@ namespace PepperDash.Essentials
private readonly Dictionary<string, IMobileControlMessenger> _defaultMessengers =
new Dictionary<string, IMobileControlMessenger>();
/// <summary>
/// Get the custom messengers with subscriptions
/// </summary>
public ReadOnlyDictionary<string, IMobileControlMessengerWithSubscriptions> Messengers => new ReadOnlyDictionary<string, IMobileControlMessengerWithSubscriptions>(_messengers.Values.OfType<IMobileControlMessengerWithSubscriptions>().ToDictionary(k => k.Key, v => v));
/// <summary>
/// Get the default messengers
/// </summary>
public ReadOnlyDictionary<string, IMobileControlMessengerWithSubscriptions> DefaultMessengers => new ReadOnlyDictionary<string, IMobileControlMessengerWithSubscriptions>(_defaultMessengers.Values.OfType<IMobileControlMessengerWithSubscriptions>().ToDictionary(k => k.Key, v => v));
private readonly GenericQueue _transmitToServerQueue;
private readonly GenericQueue _transmitToClientsQueue;
@@ -1199,8 +1212,7 @@ namespace PepperDash.Essentials
if (_initialized)
{
this.LogDebug("Registering messenger {messengerKey} AFTER initialization", messenger.Key);
messenger.RegisterWithAppServer(this);
RegisterMessengerWithServer(messenger);
}
}
@@ -1241,6 +1253,12 @@ namespace PepperDash.Essentials
messenger.MessagePath
);
if (messenger is IMobileControlMessengerWithSubscriptions subMessenger)
{
subMessenger.RegisterWithAppServer(this, Config.EnableMessengerSubscriptions);
return;
}
messenger.RegisterWithAppServer(this);
}