MessageHandler

in namespace DotVVM.BusinessPack.Messaging

Usage & Scenarios

Defines a handler for a client-side method call in the SignalR hub.

Sample 1: MessageHandler inside MessagingConnection

You can define handlers for individual methods inside the MessagingConnection control.

Use the MethodName property to specify the name of the method on the client hub (case-sensitive).

The Command property defines the command which will be triggered when the client method is called from the server.

Make sure the command is a lambda function with correct argument types. DotVVM has no way of checking the type safety on this place.

To send message in the hub, you can use the following code snippet (in the server-side code):

public async Task SendMessage(IHubContext<ChatHub> chatHub, ChatMessageDTO message) 
{
    await chatHub.Clients.All.SendAsync("IncomingMessage", message);
}
<bp:MessagingConnection ServiceUrl="/hubs/Chat">

    <bp:MessageHandler MethodName="IncomingMessage" 
                       Command="{staticCommand: (ChatMessageDTO o) => Messages.Add(o)}" />

</bp:MessagingConnection>
public class SampleViewModel : DotvvmViewModelBase
{
    public List<ChatMessageDTO> Messages { get; set; } = new();
}

public class ChatMessageDTO 
{
    public string Text { get; set; }
}

Sample 1: MessageHandler inside MessagingConnection

Sometimes, the MessagingConnection control is defined on some other place (for example in the master page), so it is not possible to place the MessageHandler inside.

You can define ConnectionId property on both MessagingConnection and MessageHandler controls which will bind these two controls together.

<bp:MessagingConnection ServiceUrl="/hubs/Chat" 
                        ConnectionId="Chat" />

...

<bp:MessageHandler MethodName="IncomingMessage" 
                   ConnectionId="Chat" 
                   Command="{staticCommand: (ChatMessageDTO o) => Messages.Add(o)}" />
public class SampleViewModel : DotvvmViewModelBase
{
    public List<ChatMessageDTO> Messages { get; set; } = new();
}

public class ChatMessageDTO 
{
    public string Text { get; set; }
}

Properties

Name Type Description Notes Default Value
property icon ConnectionId String
attribute
inner element
static value
bindable
default
null
property icon Group String
attribute
inner element
static value
bindable
default
null
property icon MethodName String
attribute
inner element
static value
bindable
default
null

Events

Name Type Description
event icon Command ICommandBinding

HTML produced by the control