Replies: 2 comments 3 replies
-
What are you using for the |
Beta Was this translation helpful? Give feedback.
0 replies
-
FYI, if you're adding code into the ASP.NET Core HTTP pipeline, you should have two easy options:
However, neither of these actually helps determine how many active subscriptions there are, especially important if you have multiple available subscriptions or if you run queries/mutations over WebSocket connections. I would write a custom // Original observable
IObservable<int> source;
// Reference count for active subscriptions
int refCount = 0;
// Hook into Subscribe and Dispose with ref counting
IObservable<int> hookedObservable = Observable.Create(observer =>
{
// Increment the reference count atomically
if (Interlocked.Increment(ref refCount) == 1)
{
// Code to run when the first subscription is made
Console.WriteLine("First subscription setup");
}
Console.WriteLine("Subscribed");
// Subscribe to the original observable
try
{
var subscription = source.Subscribe(
observer.OnNext,
observer.OnError,
observer.OnCompleted
);
// Return a disposable that hooks into the Dispose method
return Disposable.Create(() =>
{
Console.WriteLine("Disposed");
// Decrement the reference count atomically
if (Interlocked.Decrement(ref refCount) == 0)
{
// Code to run when the last subscription is disposed
Console.WriteLine("Last subscription cleaned up");
}
subscription.Dispose();
});
}
catch
{
Console.WriteLine("Failed to set up subscription");
// Decrement the reference count atomically
if (Interlocked.Decrement(ref refCount) == 0)
{
// Code to run when the last subscription is disposed
Console.WriteLine("Last subscription cleaned up");
}
throw;
}
}); |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We use GraphQL subscriptions to show live data, and we use a cloud service to feed live data to these subscriptions. Since cloud services are paid for based on usage, we would like to feed the graphql subscriptions only when there are someone watching. So if a user opens one of the web sites that contains subscriptions, this would activate the "data feeder".
To do that I need to know if there are any active subscriptions, like this variable.
I have got into the http middleware pipeline, so I can get the websocket requests there. But I am not able to figure out when the websocket connection closes. That is, catch the exception that is supposed to come when a user closes the web browser and abruptly closes the websocket connection.
Does anyone know how to get a list of active subscriptions from GraphQL?
Beta Was this translation helpful? Give feedback.
All reactions