7
votes

I need to send an instant message from the server to the client after the user has submitted a form in a browser.

I followed the Microsoft steps here to set up a signalR connection, created a Hub class, signalr.js etc.

The problem is that I can only invoke a message to all clients, but I need to invoke the message to the specific caller who initiated the request (otherwise everyone will get the message).

This is my POST Action in the HomeController.cs:

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Submit(string signalRconnectionId, Dictionary<string, string> inputs)
    {
        //Invoke signal to all clients sending a message to initSignal WORKS FINE
        await _signalHubContext.Clients.All.SendAsync("initSignal", "This is a message from the server!");

        //Invoke signal to specified client where signalRConnectionId = connection.id DOES NOT WORK
        await _signalHubContext.Clients.Client(signalRconnectionId).SendAsync("initSignal", "This is a message from server to this client: " + signalRconnectionId);

        return RedirectToAction("Success", inputs);
    }

my client javascript file:

    //Create connection and start it
const connection = new signalR.HubConnectionBuilder()
    .withUrl("/signalHub")
    .configureLogging(signalR.LogLevel.Information)
    .build();
connection.start().catch(err => console.error(err.toString()));

console.log("connectionID: " + connection.id);
$("#signalRconnectionId").attr("value", connection.id);

//Signal method invoked from server
connection.on("initSignal", (message) => {

    console.log("We got signal! and the message is: " + message);


});

I have tried debugging the action method and I get correctly passed in the connectionId which is "0" (incrementing by 1 per connection)

2
On the page you linked, there is another link that explains Clients : docs.microsoft.com/en-us/aspnet/core/signalr/…Neil
Clients.Caller.SendAsync("ReceiveMessage", message); - This will invoke the client side function only for the client who initiated the request.Thangadurai
Have you tried it yourself? When I use _signalHubContext.Clients there is no caller method available. If I do it from the Hub class there is, but then I need to put business logic in there, I would like to do it from the hubcontext I got through DI in the controllerMorten_564834
Where is this assumption from: connectionId which is "0" (incrementing by 1 per connection)aaron

2 Answers

6
votes

So here's the final solution I came up with inspired by the answer from this thread

I got the connectionId by calling the Hub class from client and then from client calling the controller passing in the connectionId.

Hub class:

public class SignalHub : Hub
{
    

    public string GetConnectionId()
    {
        return Context.ConnectionId;
    }
}

client-side javascript code executed at startup:

connection.start().catch(err => console.error(err.toString())).then(function(){
connection.invoke('getConnectionId')
    .then(function (connectionId) {
        // Send the connectionId to controller
        console.log("connectionID: " + connectionId);
        $("#signalRconnectionId").attr("value", connectionId);
    });
});

HomeController.cs:

public async Task<IActionResult> Submit(string signalRconnectionId, Dictionary<string, string> inputs)
    {
        
        //Invoke signal to specified client WORKS NOW
        await _signalHubContext.Clients.Client(signalRconnectionId).SendAsync("initSignal", "This is a message from server to this client: " + signalRconnectionId);

        return RedirectToAction("Success", inputs);
    }

It works fine, but still feels a little like a roundtrip, it would have been easier if we didn't have to go through the hub class to make this happen. Maybe just having the connectionId from the client-side to begin with, but maybe there is a good reason for the design :)

0
votes

According to Microsoft, you can not access to the ConnectionId and Caller from outside a hub https://docs.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-2.1

When hub methods are called from outside of the Hub class, there's no caller associated with the invocation. Therefore, there's no access to the ConnectionId, Caller, and Others properties.