0
votes

I'm attempting to send a message from an Azure Function to an Azure SignalR Service and its returning a 415 but I'm unsure on how to identify the cause of the issue.

I already have a working implementation for the front end of the application with users and actions able to broadcast messages through the SignalR Service however I now need a backend function to notify the users when it has completed.

The example I've followed is the one here; https://github.com/Microsoft/MTC_WhackAMole.NET/blob/master/MoleDeploy.SignalR/AzureSignalR.cs

When executing the function, it successfully completes the SendAsync() function but doesn't actually trigger anything.

var payload = new PayloadMessage()
{
    Target = methodName,
    Arguments = args
};
var url = $"{endpoint}/api/v1/hubs/{hubName}";
var bearer = GenerateJwtBearer(null, url, null, DateTime.UtcNow.AddMinutes(30), accessKey);
await PostJsonAsync(url, payload, bearer);

It's only when trying the same request in Postman that I get the 415 error.

In Postman, I'm posting to the following url;

https://xxxxxx.service.signalr.net/api/v1/hubs/{hubName}

Using a bearer authentication token (that I know is correct as changing it generates a 401 as apposed to the 415)

Its sending the following payload;

{"Target":"hubFunctionName","Arguments":"message to broadcast"}

What I'm not entirely sure on is the posted body. Should the Target be the function in the hub and the Arguments be the message to send?

Looking at the payload that is sent in PostJsonAsync() it comes out as;

payload {AzureFunctionName.AzureSignalR.PayloadMessage}
--Arguments {object[1]}
----[0] "message to broadcast"
--Target    "hubFunctionName"

The function in the hub is super basic;

public void Send(string messageText)
{
    Clients.All.broadcastMessage(messageText);
}

If anyone could point me in the right direction with this it would be greatly appreciated.

Thanks, Tom

2

2 Answers

0
votes

Based on the docs, I believe arguments has to be an array of messages to be sent instead. Could you confirm you are testing the same in Postman.

Also, instead of writing this code yourself, Azure Functions v2 has an Azure SignalR Service Output Binding that you can use directly.

0
votes

Thanks for the response - I'm still struggling to get my head around the functionality for this I guess.

To expand, I've got a basic Azure Function that reads from a queue, and I need it to get a message to my signalr service.

The queue read is straight forward (and works)

public static void Run([QueueTrigger("signalr-messages", Connection = "Azure.Storage.Main")]string myQueueItem, ILogger log)
{
    <Deserialise>
    SendMessage(message, null);
}

My SendMessage function looks like;

public static Task SendMessage(Message message, [SignalR(HubName = "baseHub")]IAsyncCollector<SignalRMessage> signalRMessages)
{
    return signalRMessages.AddAsync(
    new SignalRMessage
      {
        Target = "sendMessage",
        Arguments = new[] { message }
      });
}

I'm passing "null" in SendMessage however I know this is wrong - The examples I can find come from HTTP Triggers and not Queue Triggers

Am I even going down the right path with this?