Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's a good way to detect that a caller has disconnected from a ViewModel? #151

Closed
bugged84 opened this issue Nov 23, 2018 · 7 comments
Closed
Labels

Comments

@bugged84
Copy link

I'm trying to detect when users connect and disconnect from my VM. Detecting the connection is easy using the OnRouted handler.

this.OnRouted((sender, e) =>
{
    var userId = hubCallerContextAccessor.CallerContext.ConnectionId;
	
    // handle user entry
    m_users.Add(userId);
	
    // subscribe to user exit
    m_hubCallerContextAccessor.CallerContext.ConnectionAborted.Register(() =>
    {
        m_users.Remove(userId); // never gets called...
    }    
});

However, I'm not able to come up with a reliable way to know on the server when a user disconnects.

Do I have to resort to adding a KeepAlive action on the VM that the React component calls on regular intervals, and then the VM removes any user that has not reported in recently?

@dsuryd
Copy link
Owner

dsuryd commented Nov 23, 2018 via email

@bugged84
Copy link
Author

bugged84 commented Nov 23, 2018

That was one of the options I considered. However, that would not be purely server side detection. For example, what if the user's device suddenly crashes or loses internet? I want the server to be able to detect this and boot the user from the chat room. Depending on a react component for this detection will introduce an unstable chat room. This problem only becomes worse if, for example, you're dealing with a game that has active players.

A keep-alive approach is the only one I could think of that would handle my scenario above. If a user suddenly stops calling in, the server would be able to notice and boot the user without an explicit call from the user to do so.

Thoughts on that?

@dsuryd
Copy link
Owner

dsuryd commented Nov 23, 2018

I don’t have it off the top of my head, but there could be an event that SignalR provides for such scenario. Check their doc, then we’ll figure out how to access it from the VM.

@bugged84
Copy link
Author

SignalR hubs have the following lifecycle methods that can overridden.

public override Task OnConnected()
{
    return base.OnConnected();
}

public override Task OnDisconnected()
{
    //custom logic here
    return base.OnDisconnected();
}

public override Task OnReconnected()
{
    return base.OnReconnected();
}

@dsuryd
Copy link
Owner

dsuryd commented Nov 23, 2018 via email

@bugged84
Copy link
Author

Also, apparently the MulticastVm.Dispose() method is called each time one of these sudden disconnections happen. I moved my "remove" logic to the Dispose method and it seems to be working as expected.

@dsuryd
Copy link
Owner

dsuryd commented Nov 23, 2018

Yes, of course, that what I should have mentioned to you from the beginning :). Need to put this in the doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants