Class MessageHub
Messenger hub responsible for taking subscriptions/publications and delivering of messages.
Implements
Inherited Members
Namespace: Swan.Messaging
Syntax
public sealed class MessageHub : IMessageHub
Examples
The following code describes how to use a MessageHub. Both the subscription and the message sending are done in the same place but this is only for explanatory purposes.
class Example
{
using Swan;
using Swan.Components;
static void Main()
{
// using DependencyContainer to create an instance of MessageHub
var messageHub = DependencyContainer
.Current
.Resolve<IMessageHub>() as MessageHub;
// create an instance of the publisher class
// which has a string as its content
var message = new MessageHubGenericMessage<string>(new object(), "SWAN");
// subscribe to the publisher's event
// and just print out the content which is a string
// a token is returned which can be used to unsubscribe later on
var token = messageHub
.Subscribe<MessageHubGenericMessage<string>>(m => m.Content.Info());
// publish the message described above which is
// the string 'SWAN'
messageHub.Publish(message);
// unsubscribe, we will no longer receive any messages
messageHub.Unsubscribe<MessageHubGenericMessage<string>>(token);
Terminal.Flush();
}
}
Methods
Publish<TMessage>(TMessage)
Publish a message to any subscribers.
Declaration
public void Publish<TMessage>(TMessage message)
where TMessage : class, IMessageHubMessage
Parameters
Type | Name | Description |
---|---|---|
TMessage | message | Message to deliver. |
Type Parameters
Name | Description |
---|---|
TMessage | Type of message. |
PublishAsync<TMessage>(TMessage)
Publish a message to any subscribers asynchronously.
Declaration
public Task PublishAsync<TMessage>(TMessage message)
where TMessage : class, IMessageHubMessage
Parameters
Type | Name | Description |
---|---|---|
TMessage | message | Message to deliver. |
Returns
Type | Description |
---|---|
Task | A task with the publish. |
Type Parameters
Name | Description |
---|---|
TMessage | Type of message. |
Subscribe<TMessage>(Action<TMessage>, Boolean, IMessageHubProxy)
Subscribe to a message type with the given destination and delivery action. Messages will be delivered via the specified proxy.
All messages of this type will be delivered.
Declaration
public MessageHubSubscriptionToken Subscribe<TMessage>(Action<TMessage> deliveryAction, bool useStrongReferences = true, IMessageHubProxy proxy = null)
where TMessage : class, IMessageHubMessage
Parameters
Type | Name | Description |
---|---|---|
Action<TMessage> | deliveryAction | Action to invoke when message is delivered. |
Boolean | useStrongReferences | Use strong references to destination and deliveryAction. |
IMessageHubProxy | proxy | Proxy to use when delivering the messages. |
Returns
Type | Description |
---|---|
MessageHubSubscriptionToken | MessageSubscription used to unsubscribing. |
Type Parameters
Name | Description |
---|---|
TMessage | Type of message. |
Subscribe<TMessage>(Action<TMessage>, Func<TMessage, Boolean>, Boolean, IMessageHubProxy)
Subscribe to a message type with the given destination and delivery action with the given filter. Messages will be delivered via the specified proxy. All references are held with WeakReferences Only messages that "pass" the filter will be delivered.
Declaration
public MessageHubSubscriptionToken Subscribe<TMessage>(Action<TMessage> deliveryAction, Func<TMessage, bool> messageFilter, bool useStrongReferences = true, IMessageHubProxy proxy = null)
where TMessage : class, IMessageHubMessage
Parameters
Type | Name | Description |
---|---|---|
Action<TMessage> | deliveryAction | Action to invoke when message is delivered. |
Func<TMessage, Boolean> | messageFilter | The message filter. |
Boolean | useStrongReferences | Use strong references to destination and deliveryAction. |
IMessageHubProxy | proxy | Proxy to use when delivering the messages. |
Returns
Type | Description |
---|---|
MessageHubSubscriptionToken | MessageSubscription used to unsubscribing. |
Type Parameters
Name | Description |
---|---|
TMessage | Type of message. |
Unsubscribe<TMessage>(MessageHubSubscriptionToken)
Unsubscribe from a particular message type.
Does not throw an exception if the subscription is not found.
Declaration
public void Unsubscribe<TMessage>(MessageHubSubscriptionToken subscriptionToken)
where TMessage : class, IMessageHubMessage
Parameters
Type | Name | Description |
---|---|---|
MessageHubSubscriptionToken | subscriptionToken | Subscription token received from Subscribe. |
Type Parameters
Name | Description |
---|---|
TMessage | Type of message. |