The heartbeat feature allows for maintaining connection and early detection of disconnections by sending messages periodically from the server to client and client to server. The server-to-client heartbeat can have a specified timeout to disconnect if the client does not respond within a certain time frame.
API
Client
The client heartbeat can be specified when creating the StreamingHubClient. It is disabled by default.
// Send a message to the server every 30 seconds
var options = StreamingHubClientOptions.CreateWithDefault().WithHeartbeatInterval(TimeSpan.FromSeconds(30));
var hub = await StreamingHubClient.ConnectAsync<IChatHub, IChatHubReceiver>(channel, receiver, options);
Server
The server-side heartbeat can be enabled globally through MagicOnionOptions or via the Heartbeat attribute. It is disabled by default.
// Enable heartbeat for all StreamingHub instances
options.EnableStreamingHubHeartbeat = true;
// Send heartbeat every 30 seconds, disconnect if no response within 5 seconds
options.StreamingHubHeartbeatInterval = TimeSpan.FromSeconds(30);
options.StreamingHubHeartbeatTimeout = TimeSpan.FromSeconds(5);
// Enable heartbeat using the interval and timeout specified in MagicOnionOptions
[Heartbeat]
public class MyHub : StreamingHubBase<IMyHub, IMyHubReceiver>
{
}
// Enable heartbeat and override the interval and timeout specified in MagicOnionOptions
[Heartbeat(Interval = 10 * 1000, Timeout = 1000)]
public class MyHub : StreamingHubBase<IMyHub, IMyHubReceiver>
{
}
Additional Metadata
Additional metadata can be added to the heartbeat messages from the server. This can be used, for example, to include the server time for synchronization purposes.
Server
To add metadata to the server's heartbeat messages, implement the IStreamingHubHeartbeatMetadataProvider interface and register it with the DI container or specify it in the MetadataProvider property of the Heartbeat attribute.
public class CustomHeartbeatMetadataProvider : IStreamingHubHeartbeatMetadataProvider
{
public bool TryWriteMetadata(IBufferWriter<byte> writer)
{
MessagePackSerializer.Serialize(writer, DateTimeOffset.UtcNow);
return true;
}
}
Client
On the client side, a callback for the heartbeat can be set as an option when creating the StreamingHubClient.
var options = StreamingHubClientOptions.CreateWithDefault().WithHeartbeatReceived(x =>
{
var serverDateTime = MessagePackSerializer.Deserialize<DateTimeOffset>(x);
});
var hub = await StreamingHubClient.ConnectAsync<IChatHub, IChatHubReceiver>(channel, receiver, options);
This PR adds a heartbeat feature to StreamingHub.
The heartbeat feature allows for maintaining connection and early detection of disconnections by sending messages periodically from the server to client and client to server. The server-to-client heartbeat can have a specified timeout to disconnect if the client does not respond within a certain time frame.
API
Client
The client heartbeat can be specified when creating the
StreamingHubClient
. It is disabled by default.Server
The server-side heartbeat can be enabled globally through
MagicOnionOptions
or via theHeartbeat
attribute. It is disabled by default.Additional Metadata
Additional metadata can be added to the heartbeat messages from the server. This can be used, for example, to include the server time for synchronization purposes.
Server
To add metadata to the server's heartbeat messages, implement the
IStreamingHubHeartbeatMetadataProvider
interface and register it with the DI container or specify it in theMetadataProvider
property of theHeartbeat
attribute.Client
On the client side, a callback for the heartbeat can be set as an option when creating the StreamingHubClient.