locked
Sending a message to an Azure SignalR service from an Azure Function RRS feed

  • Question

  • User-1202235188 posted

    Hi All,

    I'm having some trouble getting my head round/finding an example on how to do this.

    Currently I've got an Azure SignalR service that is working when triggered from my front end javascript as such;

    notifications.server.sendMessage(userId, "Message");

    I now have the need for my Azure Function to send messages to the SignalR service too and this is where I'm struggling.

    With the following approach I get a 404 error, but I'm convinced the URL, hub name and method name are correct;

    public static async void Run([QueueTrigger("signalr-messages", Connection = "Azure.Storage.Main")]string myQueueItem, ILogger log)
    {
        Message message = DeserialiseQueueMessage(myQueueItem);
    
        HubConnection hub = new HubConnection("https://xxxxxxx.service.signalr.net/AzureSignalrPath", false);
        var proxy = hub.CreateHubProxy("BaseHub");
        await hub.Start();
        await proxy.Invoke("SayAnything");
    }

    With this next approach I'm unsure as to what I should be passing into the function (Obviously the "null" Im currently passing is wrong)

    public static async void Run([QueueTrigger("signalr-messages", Connection = "Azure.Storage.Main")]string myQueueItem, ILogger log)
    {
        Message message = DeserialiseQueueMessage(myQueueItem);
        SendMessage(null);
    }
    
    public static Task SendMessage([SignalR(HubName = "baseHub")]IAsyncCollector<SignalRMessage> signalRMessages)
    {
        return signalRMessages.AddAsync(
        new SignalRMessage
        {
            Target = "sendMessage"
        });
    }

    The last approach I've attempted is using the AzureSignalR class, however I also can't get this to work.

    private static async Task SendMessage(Message message)
    {
        var signalR = new AzureSignalR(Environment.GetEnvironmentVariable("AzureSignalRConnectionString"));
        await signalR.SendAsync("baseHub", "SayAnything");
    }

    Does anyone have any experience with these approaches, or can suggest something to point me in the right direction?

    Many Thanks,

    Tom

    Monday, April 15, 2019 3:05 PM

Answers

  • User283571144 posted

    Hi Tom Kirkland,

    Tom Kirkland

    HubConnection hub = new HubConnection("https://xxxxxxx.service.signalr.net/AzureSignalrPath", false);

    Could you please tell me where you hosted your signalr hub? In local IIS ? or Azure web app?

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 16, 2019 9:15 AM

All replies

  • User283571144 posted

    Hi Tom Kirkland,

    Tom Kirkland

    HubConnection hub = new HubConnection("https://xxxxxxx.service.signalr.net/AzureSignalrPath", false);

    Could you please tell me where you hosted your signalr hub? In local IIS ? or Azure web app?

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 16, 2019 9:15 AM
  • User-1202235188 posted

    Brando!

    Thank you, you've just solved the issue for me.

    HubConnection hub = new HubConnection("URL to signalr service");
    var proxy = hub.CreateHubProxy("BaseHub");
    hub.Start().Wait();
    proxy.Invoke("Send", message.MessageText);

    The above was failing me for me, until I I saw your comment and changed it to be;

    HubConnection hub = new HubConnection("http://localhost:57690/");
    var proxy = hub.CreateHubProxy("BaseHub");
    hub.Start().Wait();
    proxy.Invoke("Send", message.MessageText);

    I figured I would send message to the SignalR service, not to the application which would THEN send to the SignalR service

    Thank you!

    Tuesday, April 16, 2019 9:45 AM