1
votes

I have written an Application where I am using SignalR. I am sending connectionId from Client to Server(controller).

Everything is working fine with single browser (request will sent to server with connectionId="conn_1") and signalR is sending response to only conn_1, but when i open new browser and send a request from that client the previous connection gets disposed. Which means only one connection with particular connectionId remains alive.

Is there any way SignalR can not dispose and send response to both with data they want?

I am new to SignalR and would really appropriate any help or guidance.

Angular SignalRService to start connection with server

  this.hubConnection = new signalR.HubConnectionBuilder()
    .withUrl(this.paymentDraftHubUrl)
    .build();

  return this.hubConnection
    .start()
    .then(() => this.hubConnectionStatus = 'Connection started')
    .catch(err => (this.hubConnectionStatus = 'Error while starting connection: ' + err));
}

sending connectionId from client component to Api

this.signalRService.startConnection().then((connection) => {
      this.connectionId = connection.toString();

      //Calling Api
      this.getAllTransactionException(
        this.connectionId,
        this.pageNumber,
        this.pageSize
}

MyHub class in C#

public class PaymentDraftServiceHub : Hub, IPaymentDraftHub
{}

Controller for API using timer to keep calling repository for new data,

[HttpGet]
[Route("GetCsrTranactions")]
public IActionResult GetCsrTranactions([FromQuery] TransactionExceptionDataRequest queryParams)
{
    TimeManager.Dispose();
    var timerManager = new TimeManager(async () =>
    await _paymentDraftHub.Clients.Clients.Client(queryParams.ConnectionId).SendAsync(SignalRConstants.TransferPaymentDraftServiceData, await _paymentTransactionRepository.GetCsrTranactionsAsync(queryParams)));
    var response = new ResponseMessage { Message = "Accepted", Code = "201" };
    return Ok(response);
}
1
You mean you open new tab ?Tony Ngo
yes you are right. Opening new tabtt0206
that is normal behavior you can have new connection ìf you open another browserTony Ngo

1 Answers

2
votes

Client can have multiple connections with multiple connection IDs if client connect from multiple browser windows or tabs.

According to the code you provided, we can find that you just pass connection ID of SignalR client within current active browser tab/window to your controller, and in your controller action, you use this code snippet .Client(queryParams.ConnectionId).SendAsync() to send message to a specific client, so other browser windows or tabs would not receive the message.

If you'd like to send message(s) to a client with multiple connections, you need to map SignalR users to connection Ids and retain information about users-to-connectionIds mapping, then you can get all connectionIds of a client and send messages to that client with with multiple connectionIds, like below.

//code logic here 
//to get all connectinIds of a client/user
//from user-to-connectionIds mapping table

await _paymentDraftHub.Clients.Clients(connectionIds_here).SendAsync("method_here",args_here);