Base solution for your next web application
Open Closed

SignalR connectionId unexpected behavior #4564


User avatar
0
carelearning created

Hello,

We are trying to implement SignalR changes in order to alert and sync different simultaneous users. We have followed [https://aspnetboilerplate.com/Pages/Documents/SignalR-Integration]) and have a mysterious issue.
We first posted here [https://forum.aspnetboilerplate.com/viewtopic.php?f=5&t=10680]) and found it initially worked. Upon further testing, we found that occasionally the hub method (see below)

Clients.Others.saved(payload);

was broadcasting to the original sending client and not to the other clients. To be explicit we altered the save method to this:

public void Save(string payload)
{
	var ownerId = Context.ConnectionId;
	Clients.All.saved(new KeyValuePair<string, string>(ownerId, payload));
}

and on the client we are attempting to filter on the connectionId like this:

function processChangeFromAnotherUser(kvp) {
	  var id = kvp.Key;
	  console.log('owner', id);

	  var connectionId = hub.connection.id;
	  console.log('connectionId', connectionId);

	  if (connectionId === id) {
		  return;
	  };

	  var data = JSON.parse(kvp.Value);
  }

Unfortunately we are seeing the same unexpected behavior. We are testing using two different browsers (Chrome 63 on the left and Firefox Dev Edition (quantum) v59 on the right ). The website is hosted by IIS Express on a developer's computer. The "saved" message at the top of the screen should appear only on the right browser, but it appears on the left instead. As you can see the right browser has a connectionId which matches the sender's ID (which should be the browser on the left). Please see a screenshot here

Do you have any ideas as to what could be responsible for this unexpected behavior?


4 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Is the client calling Save in more than one place?

  • User Avatar
    0
    carelearning created

    @aaron we are not calling save in multiple places. However, we believe we have a fix in place and will let you know later this afternoon if it works. Thanks.

  • User Avatar
    0
    carelearning created

    We inverted the filtering by passing the owner connection id (aka the sender) to the server. Not certain the cause but intermittently it appeared that the SignalR Hub's Context.ConnectionId was wrong.

    Here is our work-around

    Server hub method

    public void Save(string id, string payload)
    {
         Clients.AllExcept(id).saved(payload);
    }
    

    client invoking method

    function processIndividuals() {
         // elided
        var connectionId = $.connection.hub.id;
        abp.services.app.enrollment.save(connectionId, data).done(...)
    };
    

    server application service method

    public async Task<Result<Dictionary<long, long>>> SaveAsync (string id, JObject data) {
         // elided
        var payload = JsonConvert.SerializeObject(result);
         _hub.Save(id, payload);   // invokes ISaved<string> interface implemented by SignalR hub
    }
    

    HTH

  • User Avatar
    0
    alper created
    Support Team

    thanks for the feedback ;)