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 bothMessagingConnection
andMessageHandler
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 | |
---|---|---|---|---|---|
ClientIDMode | ClientIDMode | Gets or sets the client ID generation algorithm. |
attribute
static value
|
Static | |
ConnectionId | String |
attribute
static value
bindable
|
null | ||
DataContext | Object | Gets or sets a data context for the control and its children. All value and command bindings are evaluated in context of this value. The DataContext is null in client-side templates. |
attribute
bindable
|
null | |
ID | String | Gets or sets the control client ID within its naming container. |
attribute
static value
bindable
|
null | |
IncludeInPage | Boolean | Gets or sets whether the control is included in the DOM of the page. |
attribute
bindable
|
True | |
IsConnected | Boolean |
attribute
static value
bindable
|
False | ||
MessageHandlers | List<MessageHandler> |
inner element
static value
bindable
default
|
null | ||
ReconnectAttempt | Int32 |
attribute
static value
bindable
|
30 | ||
ReconnectTimeout | Int32 |
attribute
static value
bindable
|
30 | ||
ServiceUrl | String |
attribute
static value
bindable
|
/__dotvvm_businesspack_messaging/default | ||
TryReconnect | Boolean |
attribute
static value
bindable
|
True |
Events
Name | Type | Description | |
---|---|---|---|
Connected | ICommandBinding | ||
Disconnected | ICommandBinding | ||
Error | ICommandBinding | ||
Reconnecting | ICommandBinding |