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

SignalRTrigger not working for either connection or messages category #1122

Closed
Artur-Biniek opened this issue Nov 22, 2020 · 16 comments
Closed

Comments

@Artur-Biniek
Copy link

Artur-Biniek commented Nov 22, 2020

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:
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/3.1.7/signalr.min.js"></script>

@zackliu
Copy link
Member

zackliu commented Nov 23, 2020

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.

@Artur-Biniek
Copy link
Author

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.

@23min
Copy link

23min commented Jan 26, 2021

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 :)

@stemixreal
Copy link

Is this declaration correct?

[FunctionName("OnConnected")]
public static void

both methods that aren't working have that sig

@kzu
Copy link

kzu commented May 28, 2021

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 added a commit to devlooped/dotnet-eventgrid that referenced this issue May 28, 2021
@zackliu
Copy link
Member

zackliu commented May 31, 2021

@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
Copy link
Member

zackliu commented May 31, 2021

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.

@agravity-philipp
Copy link

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".

@Y-Sindo
Copy link
Member

Y-Sindo commented Dec 26, 2022

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

@agravity-philipp
Copy link

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");
}

@Y-Sindo
Copy link
Member

Y-Sindo commented Jan 3, 2023

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.

@marcusfalck83
Copy link

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

@Y-Sindo
Copy link
Member

Y-Sindo commented Feb 24, 2023

@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
Copy link
Member

Y-Sindo commented Jul 5, 2023

Close as cannot repro

@Y-Sindo
Copy link
Member

Y-Sindo commented Apr 13, 2024

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

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

No branches or pull requests

9 participants