onMessage<T> method

void Function() onMessage<T>(
  1. void handler(
    1. String channel,
    2. T payload
    )
)

Register a global message handler that will be called for all messages from all subscriptions.

This is useful for logging, debugging, or handling messages globally before they reach subscription-specific handlers.

Returns a function to unregister the handler.

Example:

// Log all messages
final unsubscribe = transmit.onMessage((channel, payload) {
  print('Received message on $channel: $payload');
});

// Later, unregister
unsubscribe();

Implementation

void Function() onMessage<T>(void Function(String channel, T payload) handler) {
  _globalMessageHandlers.add(handler as void Function(String, dynamic));
  return () {
    _globalMessageHandlers.remove(handler);
  };
}