MessagingConnection

in namespace DotVVM.BusinessPack.Messaging

Usage & Scenarios

Establishes a connection to a SignalR hub and allows specifying handlers for client-side hub methods.

Sample 1: Basic connection

To establish a connection to a SignalR hub, place the MessagingConnection control in the page and specify the URL of the hub.

You can use the IsConnected property to detect whether the connection is active or not.

You can define one or more MessageHandler controls to run commands when methods are called on the hub.

The MessageHandler controls can be defined:

  • Inside this MessagingConnection control. See MessageHandler - Sample 1] for more info.

  • Outside this control. In this case, you need to set the ConnectionId property on both MessagingConnection and MessageHandler controls to the same value. See MessageHandler - Sample 2] for more info.

<bp:MessagingConnection ServiceUrl="/hubs/Chat" 
                        IsConnected="{value: IsConnected}">
    ...
</bp:MessagingConnection>
public class SampleViewModel : DotvvmViewModelBase
{
    public bool IsConnected { get; set; }
}

Sample 2: Connection events

To handle various connection lifecycle events, you can bind to the Connected and Disconnected events, and also to Reconnecting and Reconnected events.

The Error event will be triggered whenever there is an error (connecting to the hub, sending a message). Since any error means that the connection is broken, this event is typically accompanied by one of the other events.

It is recommended to use static commands since the events may be fired in a quick sequence.

<bp:MessagingConnection ServiceUrl="/hubs/Chat" 
                        Connected="{staticCommand: Events.Add("Connected")}" 
                        Disconnected="{staticCommand: Events.Add("Disconnected")}" 
                        Reconnecting="{staticCommand: Events.Add("Reconnecting")}" 
                        Reconnected="{staticCommand: Events.Add("Reconnected")}" 
                        Error="{staticCommand: Events.Add("Error")}"> 
    ...
</bp:MessagingConnection>
public class SampleViewModel : DotvvmViewModelBase
{
    public List<string> Events { get; set; } = new();
}

Sample 3: Reconnect settings

The MessagingConnection control has the TryReconnect property which specifies whether the control should try to reconnect automatically when the connection is lost.

The ReconnectTimeout property specifies the number of seconds after which the reconnect will be performed (default is 30 seconds).

The ReconnectAttempts property can be used to restrict the number of attempts for reconnection (default is 30 attempts, 0 means that the reconnects will be attempted infinitely).

<bp:MessagingConnection ServiceUrl="/hubs/Chat" 
                        TryReconnect="true" 
                        ReconnectAttempts="0" 
                        ReconnectTimeout="5"> 
    ...
</bp:MessagingConnection>

Sample 4: Send messages to the hub

You can call hub methods on the server by using the _messaging variable in the static commands bindings.

In order to reference the connection which you want to send the message, you need to set the ConnectionId of the MessagingConnection control to some value. Then, you can use this id in the binding.

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

<dot:Repeater DataSource="{value: Messages}" WrapperTagName="ul"> 
    <li>{{value: Author}}: {{value: Text}}</li>
</dot:Repeater>

<form>
    <dot:TextBox Text="{value: AutorName}" />: 
    <dot:TextBox Text="{value: NewMessage}" /> 
    <dot:Button Text="Send" 
                IsSubmitButton="true" 
                Click="{staticCommand: _messaging.GetConnection("Chat").InvokeAsync("SendMessage", AuthorName, NewMessage)}" />
</form>
public class SampleViewModel : DotvvmViewModelBase
{
    public List<ChatMessageDTO> Messages { get; set; } = new();

    public string AutorName { get; set; } = "user";

    public string NewMessage { get; set; }
}

public class ChatMessageDTO 
{
    public string Text { get; set; }
    public string Author { get; set; }
}
public class ChatHub : Hub 
{

    public async Task SendMessage(string autor, string text) 
    {
        await this.Clients.AllExcept(Context.ConnectionId)
            .SendAsync("IncomingMessage", new ChatMessageDTO() { Author = author, Text = text });
    }

}

Properties

Name Type Description Notes Default Value
property icon ConnectionId String
attribute
inner element
static value
bindable
default
null
property icon IsConnected Boolean
attribute
inner element
static value
bindable
default
False
property icon MessageHandlers List<MessageHandler>
attribute
inner element
static value
bindable
default
null
property icon ReconnectAttempt Int32
attribute
inner element
static value
bindable
default
30
property icon ReconnectTimeout Int32
attribute
inner element
static value
bindable
default
30
property icon ServiceUrl String
attribute
inner element
static value
bindable
default
/__dotvvm_businesspack_messaging/default
property icon TryReconnect Boolean
attribute
inner element
static value
bindable
default
True

Events

Name Type Description
event icon Connected ICommandBinding
event icon Disconnected ICommandBinding
event icon Error ICommandBinding
event icon Reconnecting ICommandBinding

HTML produced by the control