-
Notifications
You must be signed in to change notification settings - Fork 0
Subscriptions with Request Responses
Damian edited this page Jan 24, 2026
·
1 revision
Subscribing to a request event allows users to respond with a different object. This is useful for RPC-like communication.
RequestAsyncSubscribeRequest
using Lite.EventIpc;
public async Task SubscribeWithResponse()
{
// Subscribe to Ping events that respond with Pong
_eventAggregator.SubscribeRequest<Ping, Pong>(EventSubscribe_PingAsync);
// Send a Ping request event and await the Pong response
Ping ping = new Ping("HELLO!");
Pong? response = await _eventAggregator.RequestAsync<Ping, Pong>(ping);
if (response is not null)
{
// Received PONG!
}
}
private async Task<Pong> EventSubscribe_PingAsync(Ping ping)
{
var response = "Received PING loud and clear";
return new Pong(response);
}Borrowing from the previous example, you can specify a timeout when sending a request event. If no subscriber responds within the timeout period, null is returned.
public async Task SubscribeWithResponse()
{
// ...
// Send a Ping request event and await the Pong response
Ping ping = new Ping("HELLO!");
var timeout = TimeSpan.FromMilliseconds(3000);
Pong? response = null;
try
{
response = await _eventAggregator.RequestAsync<Ping, Pong>(ping, timeout);
}
catch (TimeoutException)
{
// Handle timeout (no response received in time)
}
// ...
}