Table of Contents

Messaging

Tharga.Communication supports two patterns: fire-and-forget (PostAsync) and request-response (SendMessage).

Fire-and-forget

The sender doesn't wait for a result.

// Client → Server
await clientCommunication.PostAsync(new TemperatureReading(22.4));

// Server → specific client
await serverCommunication.PostAsync(connectionId, new ConfigChanged());

// Server → all connected clients
await serverCommunication.PostToAllAsync(new MaintenanceWindow(start, end));

Groups

Groups are thin wrappers over SignalR groups. A server can reach a subset of clients in one send instead of posting to each connection.

await serverCommunication.AddToGroupAsync(connectionId, "team-a");
await serverCommunication.PostToGroupAsync("team-a", new ConfigChanged());
await serverCommunication.PostToGroupsAsync(["team-a", "team-b"], new ConfigChanged());
await serverCommunication.RemoveFromGroupAsync(connectionId, "team-a");

SignalR forgets group membership when a connection closes, and a reconnecting client gets a new connection id. The server has to add the connection to its groups again after each reconnect, for example by overriding ConnectAsync in its ClientStateServiceBase<T>. A connection that is in several of the groups passed to PostToGroupsAsync receives the message once per group.

Request-response

The sender waits for a typed response with a configurable timeout (default 60s).

// Server → client, expecting a response
var response = await serverCommunication.SendMessageAsync<PingRequest, PingResponse>(
    connectionId,
    new PingRequest("hello"),
    TimeSpan.FromSeconds(5));

if (response.IsSuccess)
    Console.WriteLine(response.Value.Reply);
else
    Console.WriteLine($"Failed: {response.Code} — {response.Message}");

A failed response carries one of these codes:

Code Meaning
TIMEOUT No response arrived within the timeout.
DISCONNECTED The client disconnected before it responded. Before 1.2 this waited for the full timeout and reported TIMEOUT.
REMOTE_ERROR The client could not handle the request: its handler threw, or it has no handler for the request type. Message holds the reason. Before 1.2 this came back as a success with default values.
// Client → server, expecting a response
var response = await clientCommunication.SendMessage<EchoRequest, EchoResponse>(
    new EchoRequest("hi"));

The default client-side timeout is configurable via CommunicationOptions.SendMessageTimeout.

On the client, SendMessage throws TimeoutException when no response arrives in time, and RemoteRequestException when the server could not handle the request (its handler threw, or it has no handler for the request type). Before 1.2 a failed request returned a response with default values instead.

Each request carries a request id, which the receiving side copies into its response. A response that arrives after its request timed out is logged and dropped, instead of being returned as the answer to the next request. Before 1.2 it was returned for the next request. Peers on 1.1 and earlier send no request id; their responses are still accepted and matched the way they were before, so mixed versions keep working, but the protection against late responses needs both sides on 1.2 or later.

Handlers

Handlers are discovered automatically — define a class that inherits from PostMessageHandlerBase<T> or SendMessageHandlerBase<TRequest, TResponse> and it gets registered in DI.

// Fire-and-forget
public class TemperatureHandler : PostMessageHandlerBase<TemperatureReading>
{
    public override Task Handle(TemperatureReading message)
    {
        Console.WriteLine($"Got {message.Value}");
        return Task.CompletedTask;
    }
}

// Request-response
public class PingHandler : SendMessageHandlerBase<PingRequest, PingResponse>
{
    public override Task<PingResponse> Handle(PingRequest message) =>
        Task.FromResult(new PingResponse($"Pong: {message.Message}"));
}

Handlers can sit in either the server or client project, depending on which side initiates the conversation.

Each incoming message is handled in its own dependency injection scope, on the client as well as on the server. A handler can depend on scoped services such as an EF Core DbContext, and the scope, with any disposable services in it, is disposed when the handler completes.

Loading handlers from additional assemblies

By default the assembly scan runs against the loaded assembly set. If your handlers live in a sibling package that wouldn't otherwise be scanned, opt them in:

builder.AddThargaCommunicationServer(options =>
{
    options.AdditionalAssemblies = [typeof(MyHandlerFromAnotherPackage).Assembly];
    // …
});

The same option is available on the client.