Notify SignalR Server On Client Disconnected Accidentally (Disgracefully) - c#

I am setting my GlobalHost Configuration like this by following this answer to listen when the client is unreachable:
GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(50);
GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30);
GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10);
And Overriding the OnDisconnected method in my HUB Class to set the client has been disconnected
public override Task OnDisconnected(bool stopCalled) {
/*My code for saving the information of disconnecting*/
return base.OnDisconnected(stopCalled);
}
I am using Xamarin for android as a client and calling the Stop() method by overriding the OnStop() method of my activity, like this:
protected override void OnStop()
{
//hubConnection.Stop(); previously I was using this but it takes too long to stop the hub connection in this way. so I wrote an explicit method and invoke it .
Task hubconnection = serverHub.Invoke("StopConnection", new object[] { MethodToIdentifyDevice() }).ContinueWith(r =>
{
});
base.OnStop();
}
Secondly, I have written an explicit hubmethod to invoke when to notify my server explicitly that my client has stopped working. That method works at OnStop event.
My actual problem is that what if
All of the stuff above is not able to call OnDisconnected method on activity stop or the application closed.
Is there anything I am missing which is not letting it to happen.
UPDATE:
I have tried changing the Transport level to WebSocket but it is not provided in Xamarin SDK for SignalR as mentioned in the intellisense .

Since WebSocketTransport is not available at Xamarin, I advice to use "Ping" workaround.
Implement Ping() method at server-side.
Call this method from clients periodically (say 1/2 of the timeout interval).
Within this method save ConnectionId : DateTime key/value pair to static ConcurrentDictionary at server-side.
Run background Task at server-side and check DateTime for all dictionary keys.
Remove old-ones and call appropriate code.

Related

ASP.NET Core 2.2 SignalR Buffering Calls Instead of Invoking Asynchronously

I'm writing an ASP.NET Core 2.2 C# web application that uses SignalR to take calls from JavaScript in a web browser. On the server side, I initialize SignalR like this:
public static void ConfigureServices(IServiceCollection services)
{
...
// Use SignalR
services.AddSignalR(o =>
{
o.EnableDetailedErrors = true;
});
}
and
public static void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
// Route to SignalR hubs
app.UseSignalR(routes =>
{
routes.MapHub<ClientProxySignalR>("/clienthub");
});
...
}
My SignalR Hub class has a method like this:
public class ClientProxySignalR : Hub
{
...
public async Task<IEnumerable<TagDescriptor>> GetRealtimeTags(string project)
{
return await _requestRouter.GetRealtimeTags(project).ConfigureAwait(false);
}
...
}
and on the client side:
var connection = new signalR.HubConnectionBuilder()
.withUrl("/clienthub")
.configureLogging(signalR.LogLevel.Information)
.build();
connection.start().then(function () {
...
// Enable buttons & stuff so you can click
...
}
document.getElementById("tagGetter").addEventListener("click", function (event) {
connection.invoke("GetRealtimeTags", "Project1").then(data => {
...
// use data
...
}
}
This all works as far as it goes, and it does work asynchronously. So if I click the "tagGetter" button, it invokes the "GetRealtimeTags" method on my Hub and the "then" portion is invoked when the data comes back. It is also true that if this takes a while to run, and I click the "tagGetter" button again in the meantime, it makes the .invoke("GetRealtimeTags") call again...at least in the JavaScript.
However...this is where the problem occurs. Although the second call is made in the JavaScript, it will not trigger the corresponding method in my SignalR Hub class until the first call finishes. This doesn't match my understanding of what is supposed to happen. I thought that each invocation of a SignalR hub method back to the server would cause the creation of a new instance of the hub class to handle the call. Instead, the first call seems to be blocking the second.
If I create two different connections in my JavaScript code, then I am able to make two simultaneous calls on them without one blocking the other. But I know that isn't the right way to make this work.
So my question is: what am I doing wrong in this case?
This is by design of websockets to ensure messages are delivered in exact order.
You can refer to this for more information: https://hpbn.co/websocket/
Quoted:
The preceding example attempts to send application updates to the
server, but only if the previous messages have been drained from the
client’s buffer. Why bother with such checks? All WebSocket messages
are delivered in the exact order in which they are queued by the
client. As a result, a large backlog of queued messages, or even a
single large message, will delay delivery of messages queued behind
it—head-of-line blocking!
They also suggest a workaround solution:
To work around this problem, the application can split large messages
into smaller chunks, monitor the bufferedAmount value carefully to
avoid head-of-line blocking, and even implement its own priority queue
for pending messages instead of blindly queuing them all on the
socket.
Interesting question.
I think the button should be disabled and show a loading icon on the first time it is clicked. But maybe your UI enables more than one project to be loaded at once. Just thought for a second we might have an X-Y problem.
Anyways, to answer your question:
One way you can easily deal with this is to decouple the process of "requesting" data from the process of "getting and sending" data to the user when it is ready.
Don't await GetRealtimeTags and instead start a background task noting the connection id of the caller
Return nothing from GetRealtimeTags
Once the result is ready in the background task, call a new RealtimeTagsReady method that will call the JavaScript client with the results using the connection id kept earlier
Let me know if this helps.

signalr how i can i post a message from server to caller

I am using Signalr 1.1.4 because im still using .net4 so cant upgrade to signalr 2.
Basically i want to post a message from the server to just the caller to avoid messages being sent to any client that did not start the process off.
My hub class looks like this
public class UpdateHub : Hub
{
/// <summary>
/// Sends the message.
/// </summary>
/// <param name="progressMessage">The progress message.</param>
public void SendMessage(string progressMessage)
{
Clients.Client(Context.ConnectionId).sendMessage(string.Format(progressMessage));
}
}
my javascript looks like this
// get handle to subscriptionUpload hub generated by SignalR
var updateHub = $.connection.UpdateHub;
// establish the connection to the server and start server-side operation
$.connection.hub.start();
updateHub.client.sendMessage = function (message)
{
$("container").empty();
$("container").append(message);
}
Now in my controller action method i would like to do something like this
UpdateHub hub = new UpdateHub();
hub.SendMessage("process has started");
//continue on with long process
hub.SendMessage("process has ended");
Is this even possible?
What we can find in documentation documentation:
You don't instantiate the Hub class or call its methods from your own
code on the server; all that is done for you by the SignalR Hubs
pipeline. SignalR creates a new instance of your Hub class each time
it needs to handle a Hub operation such as when a client connects,
disconnects, or makes a method call to the server.
Because instances of the Hub class are transient, you can't use them
to maintain state from one method call to the next. Each time the
server receives a method call from a client, a new instance of your
Hub class processes the message. To maintain state through multiple
connections and method calls, use some other method such as a
database, or a static variable on the Hub class, or a different class
that does not derive from Hub. If you persist data in memory, using a
method such as a static variable on the Hub class, the data will be
lost when the app domain recycles.
And then:
If you want to send messages to clients from your own code that runs
outside the Hub class, you can't do it by instantiating a Hub class
instance, but you can do it by getting a reference to the SignalR
context object for your Hub class.
You can get the context of your hub: GlobalHost.ConnectionManager.GetHubContext<YourHub>()
and then you can use it to call methods on the client side like this:
context.Clients.All.YourMethod(params);
or
context.Clients.Client(someConnectionID).YourMethod(params);
But in this case you won't be able to use Context.ConnectionId in this methods, because you don't have a direct connection to your hub. In this case you will need to store your connections somewhere (static variables, cache, db etc) and then use it to determine which client should be called.
Hope it will help.

How Can I Manually Add Connections to Groups with Multiple Hubs in SignalR

I have two hubs, call them NotificationHub and ReminderHub. Think of NotificationHub as the main hub, and ReminderHub as an optional hub that I wish to separate from NotificationHub. Clients will connect to NotificationHub with the following typical server hub method.
public override Task OnConnected()
{
return base.OnConnected()
}
with corresponding client connection
$.connection.hub.start().done(function() {
subscribeToReminderHub();
});
subscribeToReminderHub(); contains the following
subscribeToReminderHub = function() {
reminderProxy = $.connection.reminderHub;
reminderProxy.server.subscribe().done(function() {
console.log('subscribed to reminder hub...');
});
}
reminderProxy.server.subscribe() refers to the following server method on ReminderHub
public async Task Subscribe()
{
var currentUser = Context.User.Identity.Name.ToUpperInvariant();
await Groups.Add(Context.ConnectionId, currentUser);
}
This all works as I would tyically expect. I can hit a break point on the server Subscribe() method, as well as log out
subscribed to reminder hub...
However, if I try to then invoke methods on users in the groupings I am trying to establish in ReminderHub, nothing will occur. I have defined two client functions inside my initial connection .done() callback. Consider the following example
public void Notify() // ... ReminderHub
{
// ***** notification chain - step 2
// ***** this never gets called
var userId = Context.User.Identity.Name.ToUpperInvariant();
Clients.Caller.notify();
}
// **** $.connection.hub.start().done(function() { **** callback
subscribeToReminderHub = function() {
reminderProxy = $.connection.reminderHub;
reminderProxy.server.subscribe().done(function() {
console.log('subscribed to reminder hub...');
});
reminderProxy.client.queryNotifications = function () {
// ***** notification chain - step 1
// ***** this never gets called
reminderProxy.server.notify();
}
reminderProxy.client.notify = function () {
// ***** notification chain - step 3
// ***** this never gets called
}
}
Starting this notification chain, I am invoking Notify() external from the hub like... note: I am passing userId which would relate back to the grouping
GlobalHost.ConnectionManager.GetHubContext<ReminderHub>().Clients.Group(userId).queryNotifications();
The most interesting part of this whole issue
is that if I don't introduce a second hub, and establish the group on NotificationHub's OnConnected and re-factor all logic back to the sole hub, this entire process works as expected. Somehow introducing a second hub and trying to establish a group outside of OnConnected is not working. Has anyone experienced this? Thoughts?
Even more interesting! (and awful!)
if I open up my browser dev tools and explicitly paste the following into my console
reminderProxy.server.notify()
I hit the breakpoint on ReminderHub's Notify()!
I continue through Clients.Caller.notify(); and the client function .notify() does not even get called in my client JS. I can't understand that at all. Bypassing all groping concerns, I can't even hit a client function now that I've introduced ReminderHub
You should make sure you prepare all of your client-side event handlers before starting the connection, otherwise SignalR will not fulfill them (at least in its current version).

How is this code being reached in SignalR

I have the following method in my Hub class:
public class NotificationHub : Hub
{
public void SendAll(string message)
{
if (1 == 0)
Clients.All.broadcastMessage(message); // this should be unreachable
}
}
Then, I (am trying) to call that method from my server-side code like so:
GlobalHost.ConnectionManager.GetHubContext<NotificationHub>()
.Clients.All.broadcastMessage("broadcastMessage was called");
The method is called, and everything works. But, I didn't want broadcastMessage() to be called since it should have been unreachable.
I read this from the documentation:
You don't instantiate the Hub class or call its methods from your own
code on the server; all that is done for you by the SignalR Hubs
pipeline. SignalR creates a new instance of your Hub class each time
it needs to handle a Hub operation such as when a client connects,
disconnects, or makes a method call to the server.
Ref. http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server
But it doesn't look like it uses my methods at all. It just looks like it calls its own methods and ignores mine. How can I call my own methods using SignalR?
When you do this:
GlobalHost.ConnectionManager.GetHubContext<NotificationHub>()
.Clients.All.broadcastMessage("broadcastMessage was called");
You're bypassing the method
public void SendAll(string message)
I am not sure why you'd expect the first method to be blocked. If you want your hub logic to work you have to work through the hub methods (example: public void SendAll(string message))
I like the solution presented here: https://stackoverflow.com/a/17897625/693272 if you want to call the hub method from outside.

Fire and Forget (Asynch) ASP.NET Method Call

We have a service to update customer information to server. One service call takes around few seconds, which is normal.
Now we have a new page where at one instance around 35-50 Costumers information can be updated. Changing service interface to accept all customers together is out of question at this point.
I need to call a method (say "ProcessCustomerInfo"), which will loop through customers information and call web service 35-50 times. Calling service asynchronously is not of much use.
I need to call the method "ProcessCustomerInfo" asynchronously. I am trying to use RegisterAsyncTask for this. There are various examples available on web, but the problem is after initiating this call if I move away from this page, the processing stops.
Is it possible to implement Fire and Forget method call so that user can move away (Redirect to another page) from the page without stopping method processing?
Details on: http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx
Basically you can create a delegate which points to the method you want to run asynchronously and then kick it off with BeginInvoke.
// Declare the delegate - name it whatever you would like
public delegate void ProcessCustomerInfoDelegate();
// Instantiate the delegate and kick it off with BeginInvoke
ProcessCustomerInfoDelegate d = new ProcessCustomerInfoDelegate(ProcessCustomerInfo);
simpleDelegate.BeginInvoke(null, null);
// The method which will run Asynchronously
void ProcessCustomerInfo()
{
// this is where you can call your webservice 50 times
}
This was something I whipped just to do that...
public class DoAsAsync
{
private Action action;
private bool ended;
public DoAsAsync(Action action)
{
this.action = action;
}
public void Execute()
{
action.BeginInvoke(new AsyncCallback(End), null);
}
private void End(IAsyncResult result)
{
if (ended)
return;
try
{
((Action)((AsyncResult)result).AsyncDelegate).EndInvoke(result);
}
catch
{
/* do something */
}
finally
{
ended = true;
}
}
}
And then
new DoAsAsync(ProcessCustomerInfo).Execute();
Also need to set the Async property in the Page directive <%# Page Async="true" %>
I'm not sure exactly how reliable this is, however it did work for what I needed it for. Wrote this maybe a year ago.
I believe the issue is the fact is your web service is expecting a client to return the response to, that the service call itself is not a one way communication.
If you're using WCF for your webservices look at http://moustafa-arafa.blogspot.com/2007/08/oneway-operation-in-wcf.html for making a one way service call.
My two cents: IMO whoever put the construct on you that you're not able to alter the service interface to add a new service method is the one making unreasonable demands. Even if your service is a publicly consumed API adding a new service method shouldn't impact any existing consumers.
Sure you can.
I think what you are wanting is a true background thread:
Safely running background threads in ASP.NET 2.0
Creating a Background Thread to Log IP Information

Categories