3

I am using the Azure SignalR Service in combination with Azure Functions and I have the following code:

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

I have a hard time getting a trigger on the 'OnConnected' event. Samples are only given with Class based model. And the docs aren't really helpful for me.

Docs are telling me the category parameter of the SignalRTrigger constructor should be: connections or messages. So I use connections.

This value must be set as the category of messages for the function to be triggered. The category can be one of the following values: connections: Including connected and disconnected events messages: Including all other events except those in connections category

I don't really understand what the docs mean with the event parameter

This value must be set as the event of messages for the function to be triggered. For messages category, event is the target in invocation message that clients send. For connections category, only connected and disconnected is used.

I guess they are saying you can choose between connected and disconnected.

However with the code from above the trigger is never hit. Any thoughts?

gerb0n
  • 380
  • 1
  • 5
  • 19
  • I won't mark this as the answer because it's not. But I ended up using an EventGrid-trigger as suggested by the Azure team themselves: https://www.youtube.com/watch?v=VTVqV0hZ1EQ. I would still like to know how to trigger on those events using the traditional model. I also don't have any way to create groups or add clients to groups this way. – gerb0n Sep 24 '20 at 22:27
  • May help: https://github.com/Azure/azure-functions-signalrservice-extension/issues/111 – Codi Dec 07 '20 at 14:56

2 Answers2

5

Original Answer: https://learn.microsoft.com/en-us/answers/questions/159266/debug-function-using-a-signalrtrigger.html

For the SignalRTrigger to work, you need to set a webhook to the function in SignalR.

When you deploy a function with the SignalRTrigger, it does 2 extra things:

  • Create the webhook: https://<APP_NAME>.azurewebsites.net/runtime/webhooks/signalr
  • Create an API key code (signalr_extension) in the function app system settings (see "system keys" section in "App Keys" blade of the azure portal Function App)

SignalR POSTs events to this webhook (so obviously this doesn't work on local, unless you have a publicly addressable IP that you can add to SignalR).

To configure the SignalR event webhook, go to "Settings" blade of SignalR and add the upstream URL https://<APP_NAME>.azurewebsites.net/runtime/webhooks/signalr?code=<API_KEY>

Voila! This should now work

Ref: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-signalr-service-trigger?tabs=javascript#send-messages-to-signalr-service-trigger-binding

Codi
  • 511
  • 3
  • 19
2

This code snippet works.

[FunctionName("OnConnected ")]
public async Task OnConnected ([SignalRTrigger("YourHub", "connections", "connected ")]
InvocationContext invocationContext, ILogger logger)
{
    logger.LogInformation($"{invocationContext.ConnectionId} has connected");
}

Also configure, Upstream URL in Azure SignalR settings

<Function_App_URL>/runtime/webhooks/signalr?code=<API_KEY>

SignalR Service integration

The Function_App_URL can be found on Function App's Overview page and The API_KEY is generated by Azure Function. You can get the API_KEY from signalr_extension in the App keys blade of Function App.

Maciej Pszczolinski
  • 1,623
  • 1
  • 19
  • 38