azure-signalr icon indicating copy to clipboard operation
azure-signalr copied to clipboard

SignalRTrigger not working for either connection or messages category

Open Artur-Biniek opened this issue 5 years ago • 7 comments

Describe the bug

SignalRTrigger attribute with category set to either messages or connection is not invoking azure function when client connects/disconnects/calls client side connection.invoke("medhodName", params).

To Reproduce

// negotiate function (not shown here), works, also sending messages to connected clients via HTTPTriggered function also works. Tested both with class based and triggers approach. Tested on real Azure SignalR service and on local emulator from the preview package (asrs-emulator)

public class SignalRHubFunction
{
    [FunctionName("SignalRConnected")]
    public async Task Run([SignalRTrigger("chat", "connections", "connected", ConnectionStringSetting = "AzureSignalRConnectionString")] InvocationContext invocationContext, ILogger logger)
    {
        logger.LogInformation($"{invocationContext.ConnectionId} connected");
    }
}

func sdk discovers triggers:

Azure Functions Core Tools Core Tools Version: 3.0.2996 Commit hash: c54cdc36323e9543ba11fb61dd107616e9022bba Function Runtime Version: 3.0.14916.0

[2020-11-22T13:09:17.297Z] Unsupported service transport type: . Use default Transient instead.

Functions:

    negotiate:  http://localhost:7071/api/negotiate

    send: [GET] http://localhost:7071/api/chat/send

    Broadcast: signalRTrigger

    OnConnected: signalRTrigger

    OnDisconnected: signalRTrigger

Exceptions (if any)

Further technical details

Server Side: TargetFramework: netcoreapp3.1 AzureFunctionsVersion: v3 "Microsoft.Azure.WebJobs.Extensions.SignalRService" Version="1.2.2" "Microsoft.NET.Sdk.Functions" Version="3.0.11"

Client side:

Artur-Biniek avatar Nov 22 '20 13:11 Artur-Biniek

Did you get some log when triggering functions from Azure Function side or client side. Here's some possible logs that implies some issues

  1. Totally not logs from function or client side. Possible the upstream setting is empty or the filter is not correct.
  2. 4xx/5xx from console log of the browser when the client is invoking. Possible the upstream url is not accessible. Say, trying to access a localhost url. If you can't find an explicit reason, you can access my email with your ResourceID and invocation time. We can dig deeper.

zackliu avatar Nov 23 '20 06:11 zackliu

Hi I didn't have any errors in logs. Triggers were not causing my functions to run, that's it. There are no filters or settings above the connection string and CORS to test it.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureSignalRConnectionString": "Endpoint=http://localhost;Port=8088;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGH;Version=1.0;",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  },
  "Host": {
    "LocalHttpPort": 7071,
    "CORS": "http://localhost:5500",
    "CORSCredentials": true
  }

}

Please see the repo: https://github.com/Artur-Biniek/SignalRTriggerNotWorking

Backend code is in Function1.cs. There are negotiate & send functions that work. The other two do not.

I'd expect when different clients join and disconnect the warning logs to be printed (and breakpoints to be hit):

https://github.com/Artur-Biniek/SignalRTriggerNotWorking/blob/ba19d12db2e384ae2a5fb84b7a943bf7cd39f94b/Backend/RealTimeTestZero/Function1.cs#L50 and https://github.com/Artur-Biniek/SignalRTriggerNotWorking/blob/ba19d12db2e384ae2a5fb84b7a943bf7cd39f94b/Backend/RealTimeTestZero/Function1.cs#L61

But they're not. I couldn't see anything in the official documentation that would say those shouldn't work in this scenario.

FYI: the local func runtime seems to pick up those triggers and report them on the startup. Even displays them in different color to "normal" functions.

Artur-Biniek avatar Nov 23 '20 14:11 Artur-Biniek

I'm having the same problem. Using MSI. Any additional troubleshooting tips? Possibly moving to event grid for my specific use case since this feature is still in Preview and doesn't seem to get a lot of action :)

23min avatar Jan 26 '21 17:01 23min

Is this declaration correct?

[FunctionName("OnConnected")] public static void

both methods that aren't working have that sig

stemixreal avatar Jan 27 '21 23:01 stemixreal

Same issue here, against production SignalR service, tried both approaches (class and trigger), as explained in https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-signalr-service-trigger?tabs=csharp#with-class-based-model

kzu avatar May 28 '21 18:05 kzu

@stemixreal If you're using class-based model. You need to use non-static method. And the method name should be OnConnected rather than FunctionName. Please refer to https://github.com/aspnet/AzureSignalR-samples/blob/9c16f7cc98ef38b8433251b2466ecd5bd0746418/samples/BidirectionChat/csharp/Function.cs#L37

zackliu avatar May 31 '21 02:05 zackliu

OnConnected is not easy for troubleshooting. Please set a method like broadcast in Azure Function and invoke it from client. You should get an error from browser console. The status code you get comes from the invoke between service and your function. And I suggest you enabling Diagnostic Log in the service. You can see the reason why the invoke is failed.

zackliu avatar May 31 '21 02:05 zackliu

Any progress on this? I don't get any connection, broadcast or disconnected event too. Using Microsoft.Azure.WebJobs.Extensions.SignalRService Version="1.8.0".

agravity-philipp avatar Dec 25 '22 20:12 agravity-philipp

@agravity-philipp Did you check all the points that @zackliu mentioned? You could also use live trace tool to have a check.

Y-Sindo avatar Dec 26 '22 04:12 Y-Sindo

Hi @Y-Sindo ! Thanks for sharing that tool. Unfortunatly it doesn't help much. I didn't find any (working) samples on serverless SignalR "connected" event trigger. None of the above mentioned or other samples on GitHub or StackOverview are working... I can not add any parameter to the SignalRTrigger.

If I do

[FunctionName(nameof(OnConnected))]
public async Task OnConnected([SignalRTrigger("HubName", "connections", "connected")] InvocationContext invocationContext, ILogger logger)
{
    invocationContext.Headers.TryGetValue("Authorization", out var auth);
    // await Clients.All.SendAsync(NewConnectionTarget, new NewConnection(invocationContext.ConnectionId, auth));
    logger.LogInformation($"{invocationContext.ConnectionId} has connected");
}

I get this message: Microsoft.Azure.WebJobs.Host: Error indexing method 'OnConnected'. Microsoft.Azure.WebJobs.Extensions.SignalRService: SignalRTriggerAttribute must use parameterless constructor in class based model

Don't get me wrong. SignalR is working with all my clients, with grouping, direct messaging to users etc. But I just don't get the trigger on (new) connections.

This did not work too:

[FunctionName(nameof(OnConnected))]
public async Task OnConnected([SignalRTrigger] InvocationContext invocationContext, ILogger logger)
    invocationContext.Headers.TryGetValue("Authorization", out var auth);
    logger.LogInformation($"{invocationContext.ConnectionId} has connected");
}

agravity-philipp avatar Dec 26 '22 22:12 agravity-philipp

For this sample:

[FunctionName(nameof(OnConnected))]
public async Task OnConnected([SignalRTrigger("HubName", "connections", "connected")] InvocationContext invocationContext, ILogger logger)
{
    invocationContext.Headers.TryGetValue("Authorization", out var auth);
    // await Clients.All.SendAsync(NewConnectionTarget, new NewConnection(invocationContext.ConnectionId, auth));
    logger.LogInformation($"{invocationContext.ConnectionId} has connected");
}

As the error message says, you should remove all the parameters of the SignalRTrigger attribute. And the latter function you posted is correct.

There is a sample with connected trigger mentioned here. In class-based model, as long as your function name is "OnConnected", that function is recognized as a connected trigger, and the hub name is the class name where your functions belong to. Therefore, there is no need to add parameters "HubName", "connections", "connected" to the attribute.

Please tell me if you have any problem after checking the sample.

Y-Sindo avatar Jan 03 '23 02:01 Y-Sindo

Is there any update to this issue? We are facing the exact same issue in 2023.

marcusfalck83 avatar Feb 21 '23 09:02 marcusfalck83

@marcusfalck83 We keep this issue open just to help users to find troubleshooting methods of SignalR trigger easier. If the troubleshooting guide here doesn't work for you, please open a new issue and provide more information for us to help you.

Y-Sindo avatar Feb 24 '23 03:02 Y-Sindo

Close as cannot repro

Y-Sindo avatar Jul 05 '23 07:07 Y-Sindo

For users who have trouble with SignalR trigger, following docs might help you:

  1. Azure Functions development and configuration with Azure SignalR Service
  2. SignalR Service trigger binding for Azure Functions

Y-Sindo avatar Apr 13 '24 11:04 Y-Sindo