aspnet / AzureSignalR-samples

Code samples for Azure SignalR
MIT License
327 stars 390 forks source link

Possible to 'act' on a signalr function app OnDisconnected invocation? #251

Closed RotateAt60MPH closed 9 months ago

RotateAt60MPH commented 9 months ago

I have a function app project based on the DotnetIsolated-BidirectionChat sample code, running in isolated mode with a serverless SignalR resource in Azure. I'm trying to figure out a way that clients in a 'chat' get some sort of notification that the chat partner has disconnected.

The sample has a rudimentary OnDisconnected, but it does not do anything useful apart from logging:

    [Function("OnDisconnected")]
    [SignalROutput(HubName = "Hub")]
    public void OnDisconnected([SignalRTrigger("Hub", "connections", "disconnected")] SignalRInvocationContext invocationContext)
    {
        _logger.LogInformation($"{invocationContext.ConnectionId} has disconnected");
    }

Is it possible I can do ANYTHING meaningful in response to this function method being invoked by SignalR? At least the OnConnected returns a 'SignalRMessageAction' which the clients can listen for but OnDisconnected does not return anything.

I need to signal something (anything!) to connected clients that a client has disconnected. Is there anyway to do this? Maybe invoke a hub method in the OnDisconnected function method implementation? Or with the (new) Azure Event Grid integration, could an event subscriber in the Function App get a disconnect notification, which can be parlayed into a hub method invocation?

OnDisconnected appears to be somewhat 'without use' if all you can do is a log message.

Y-Sindo commented 9 months ago

OnDisconnected is like OnConnected, just a normal function. You could also return SignalRMessageAction in the function.

RotateAt60MPH commented 9 months ago

D-oh. Didn't know why I didn't think of that. Thanks.