Signalr Client results

ABP.IO
abp-community

--

ASP.NET Core 7 supports requesting a result from a client, in this article, we will show you how to use client results with the ABP framework.

Create a SignalR hub

public class ChatHub:AbpHub
{
public async Task<string> WaitForMessage(stringconnectionId
{
varmessage=awaitClients.Client(connectionId).InvokeAsync<string>("GetMessage");
returnmessage;
}
}
  • ChatHub inherits from AbpHub that has useful base properties like CurrentUser.
  • Define the WaitForMessage method to call the client's GetMessage method and get the return value.

Using InvokeAsync from a Hub method requires setting the MaximumParallelInvocationsPerClient option to a value greater than 1.

Client

Clients should return results in their .On(...) handlers.

.NET Client

hubConnection.On("GetMessage",async ()=>
{
Console.WriteLine("Enter message:");
varmessage=awaitConsole.In.ReadLineAsync()
;returnmessage;
});

JavaScript client

connection.on("GetMessage",function(){
constmessage=prompt("Enter message:");
returnmessage;
});

Strongly-typed hubs

We can use strongly-typed instead of InvokeAsync by inheriting from AbpHub<T> or Hub<T>:

public interface IClient
{
Task<string> GetMessage();
}
public class ChatHub : AbpHub<IClient>
{
public async Task<string> WaitForMessage(stringconnectionId)
{
string message = awaitClients.Client(connectionId).GetMessage();
returnmessage;
}
}

See also:

--

--

ABP.IO
abp-community

Open Source ABP Framework is a complete infrastructure to create modern web applications by following the best practices & conventions of software development.