SignalR - Client receives duplicated messages

Viewed 121

I have a problem with receiving duplicated messages on Client side.

Below is server side (ASP.NET CORE) subscribtion: So in AddClientSubscribtion method I am checking in memory clientList if passed connectionId has been already subscribed - if yes this method return false. So assigning to a group is established by fixtureId - it means that group name = fixtureId.

public async Task SubscribeFixtureActions(string fixtureId)
{
      var isAdded = _broadcastService.AddClientSubscribtion(Context.ConnectionId, SocketMessageType.NewFixtureAction);

      if (isAdded)
      {
          await Groups.AddToGroupAsync(Context.ConnectionId, fixtureId);
      }
}

Here you can find sending message method for particular group:

public void SendByMatchHub(string fixtureId, string message, string messageType)
{
    _matchHubContext.Clients.Group(fixtureId).SendAsync(messageType, message);
}

This is method written in AngularJS client side for receiving message:

  this._ws.hubConnection.on(MatchActionWSEvent.RECEIVED_NEW_ACTIONS, receivedData => {
  try {
    const receivedObject = JSON.parse(receivedData);
    this._store.dispatch(receivedNewActions(receivedObject));
  }
  catch(err) {
    console.log('RECEIVED_NEW_ACTIONS error', err)
  }
})

Client is calling SubscribeFixtureActions every time when user opens 'fixture details' page. Fixture details can be opened from 'fixture list' page so I can step to "fixture details' and then bo gack to 'fixture list' and step again to 'fixture details' so in this scenario client will call SubscribeFixtureActions two times.

So my problem is that this method is calling as many times as client subscribe to this group. But I do not understand why? Because in the server method SubscribeFixtureActions you can see that code is checking if client is already added and Groups.AddToGroupAsync is called only if ConnectionId has not been added earlier.

 this._ws.hubConnection.on(MatchActionWSEvent.RECEIVED_NEW_ACTIONS, receivedData => {

When I debugging I can see that the group has only 1 member but client "on" function is called as many time as client invoke subscribeFixtureActions method

I am not sure what I am doing wrong because from server side perspective I am sure that adding to group is executed only one time!

0 Answers
Related