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

Support closing/stopping a connection from the server side #2101

Closed
DamianEdwards opened this issue May 29, 2013 · 25 comments
Closed

Support closing/stopping a connection from the server side #2101

DamianEdwards opened this issue May 29, 2013 · 25 comments
Milestone

Comments

@DamianEdwards
Copy link
Member

Add API to the server to allow stopping a connection programatically so that applications can forcibly end a connection based on custom logic.

API should support idea of a terminal close where the client should not try auto-reconnecting when it receives the stop command, as well as a close with reconnect delay, where client will wait for the amount of time passed before attempting to reconnect.

Open questions:

  • Should client reconnect be a true reconnect (preserve connection ID) or a stop/start cycle?
  • Should the API allow choosing if the client attempts a reconnect vs. full stop/start cycle?
  • Should the API allow passing a reconnect delay?
@frankgeerlings
Copy link

It would be nice for the client to be able to subscribe to the reconnect as an event. In that case the answer to the open questions could just be 'let the client figure it out'. Would you agree?

I'm very much looking forward to this feature, as it would probably solve an issue I'm having, as described on Stack Overflow.

@davidfowl
Copy link
Member

Moving this to post 2.0.0 beta as it will cause too much churn.

@KSemenenko
Copy link

It's really a good idea! I need this functionality.

To your question, I think you need even so have separate functions for this.

But on the other hand, we can not know how to implement client (client for android or ios) but disable this client no longer needed we need. In this mostly seems to me - it's close connections of the server.

@st04
Copy link

st04 commented Mar 26, 2014

Is there any indication as to when this functionality will be integrated in to SignalR?

@davidfowl
Copy link
Member

Nope, we have a large backlog and this currently isn't on that list.

@DamianEdwards
Copy link
Member Author

We'll move this to the new repo as a candidate for v3 or beyond.

@warrenbuckley
Copy link

With you guys working on SignalR again and released 2.2.1 recently would this be considered to be looked at before V3 .NET Core version?

As was hoping if a certain event happened for a specific user. Such as log out then I could force the connection to terminate/close from the server for that user/connection or would there be an alternative approach to this @DamianEdwards or @davidfowl ?

@KSemenenko
Copy link

We are still waiting for this functionality. @DamianEdwards @davidfowl

@analogrelay
Copy link
Contributor

analogrelay commented Oct 9, 2018

We don't have any plans to add this functionality to ASP.NET SignalR at the moment. Could you provide more information about your scenario so we can get an idea of what you'd need here? You originally mentioned it in 2013 so I'm curious what problems you've encountered in the mean time that this would solve. That will help us decide if and how to take such a change.

@KSemenenko
Copy link

I usually use WebSocket as a communication channel between a server and a mobile application, now it's Xamarin. SignalR already supports scaling and it suits me. So I want to use it as a solution for communication between clients and servers. Usually these are games.
And as in 2013, in my logic, there are situations when I want to break the connection.
For example, the user logged in from another device, and this means that I no longer need to send messages over the old connection.
Or I understand that the user is trying to deceive the server and sends incorrect requests - in this case I want to cancel his authorization token and break the connection.
In general, this is a situation where I can not rely on the fact that it is my client that is connected, which, according to my command, disconnects. And I want to be able to do this on the server.

@anurse

@Grart
Copy link

Grart commented Oct 11, 2018

`

using HubCallerContextDict = System.Collections.Concurrent.ConcurrentDictionary<string, HubCallerContext>;
public class ChatHub : Hub
{
	static HubCallerContextDict _HubDict = new HubCallerContextDict();

	[Authorize]
	public async Task SendMessage(string user, string message)
	{
		await Clients
			.AllExcept(this.Context.ConnectionId)
			.SendAsync("addMessage", user, message);
	}

	public override Task OnConnectedAsync()
	{
		if (this.Context.User.Identity.IsAuthenticated)
		{
			var _userId = this.Context.User.Identity.Name;
			if (_HubDict.TryGetValue(_userId, out HubCallerContext _clientHub))
			{
				if (
					!object.Equals(_clientHub.ConnectionId, this.Context.ConnectionId)
					)
				{
					_HubDict.TryRemove(_userId, out _clientHub);
					_clientHub.Abort();
				}
			}
			_HubDict.TryAdd(_userId, this.Context);
		}
		return base.OnConnectedAsync();
	}

	public override Task OnDisconnectedAsync(Exception exception)
	{
		if (this.Context.User.Identity.IsAuthenticated)
		{
			var _userId = this.Context.User.Identity.Name;
			if (_HubDict.TryGetValue(_userId, out HubCallerContext _clientHub))
			{
				if(object.Equals(this.Context.ConnectionId, _clientHub.ConnectionId))
				{
					_HubDict.TryRemove(_userId, out _clientHub);
					_clientHub.Abort();
				}
			}
		}
		return base.OnDisconnectedAsync(exception);	
	}
}

`

@KSemenenko
Copy link

@Grart It's sample? because in 2.3 version don't have _clientHub.Abort(); method

@analogrelay
Copy link
Contributor

It looks like @Grart 's sample is using ASP.NET Core SignalR, not ASP.NET SignalR

@mbpakalin
Copy link

Any news on this functionality ? I think there is still no method to stop connection from the server side.

@analogrelay
Copy link
Contributor

We don't plan to add this functionality in ASP.NET SignalR. It is possible to stop the connection from the server side in ASP.NET Core SignalR.

@mbpakalin
Copy link

@anurse Thanks for your reply, while waiting the reply i saw this on stack overflow
And it solved my problem.

@analogrelay
Copy link
Contributor

Yep, that's a workaround available to you. Glad you found a solution!

@modmoto
Copy link

modmoto commented Jan 22, 2021

We don't plan to add this functionality in ASP.NET SignalR. It is possible to stop the connection from the server side in ASP.NET Core SignalR.

How do you do that in ASP.Net Core? I dont find any function in my Hub class or on the Clients that would allow something like Close or Disconnect.

Edit: Sorry, I am dumb. Context.Abort(); in the Hub class seems to do the trick.

@human33
Copy link

human33 commented May 12, 2021

@anurse is there a way to stop connection from server side outside of a Hub?
I'm using ASP.NET Core SignalR.

@davidfowl
Copy link
Member

When you get a new connection, store it in something and that will let you use it from outside the hub.

@human33
Copy link

human33 commented May 12, 2021

@davidfowl Thanks!

@KSemenenko
Copy link

@davidfowl
If I have 10 nodes, where do I save the connection?
And if we are talking about Azure SignalR, I don't think I have access to all connections because of scaling.
The next thing, the user at some point becomes invalid, for example he has finished all sessions and I still have a connection. Or maybe I have more than one connection and then I need to send a message to all 10 nodes to check if there is a connection for a particular user and close them?

@davidfowl
Copy link
Member

You have access to the connections on your server. If you need to disconnect a connection from any arbitrary server then it gets a bit more difficult. You need to use something like redis, publish a message to the node with the right connection.

@Gruski
Copy link

Gruski commented Jul 13, 2023

when you say save the connection to use it later do you mean the HubCallerContext? because that is what I need to call Abort() on

@davidfowl
Copy link
Member

when you say save the connection to use it later do you mean the HubCallerContext? because that is what I need to call Abort() on

That's correct.

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

No branches or pull requests