question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to send SignalR message to group via [SignalROutput] output binding

See original GitHub issue

In the negotiate, I add the current user to a group. When I trigger a message to all users, the message is received and displayed successfully in my PoC HTML/JS client. When I trigger a message to a specific group, no message is received. Even though each connection should have been added to the group.

My Poc Client

    [Function("index")]
    public static HttpResponseData GetWebPage([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequestData req)
    {
        var response = req.CreateResponse(HttpStatusCode.OK);
        response.WriteString(File.ReadAllText("content/index.html"));
        response.Headers.Add("Content-Type", "text/html");
        return response;
    }

Client html (taken from Sample)

<html>

<body>
  <h1>Azure SignalR Serverless Sample</h1>
  <div id="messages"></div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/3.1.7/signalr.min.js"></script>
  <script>
    let messages = document.querySelector('#messages');
    const apiBaseUrl = window.location.origin;
    const connection = new signalR.HubConnectionBuilder()
        .withUrl(apiBaseUrl + '/api')
        .configureLogging(signalR.LogLevel.Information)
        .build();
      connection.on('newMessage', (message) => {
        document.getElementById("messages").innerHTML = message;
      });

      connection.start()
        .catch(console.error);
  </script>
</body>

</html>

My Azure Functions

     [Function("negotiate")]
    public async Task<NegotiateGroupAction> Negotiate(
        [HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequestData req,
        [SignalRConnectionInfoInput(HubName = "hub")] SignalRConnectionInfo connectionInfo)
    {
        var signalRGroupAction = new SignalRGroupAction(SignalRGroupActionType.Add)
        {
            GroupName = "testGroup",
            UserId = "user1" 
        };

        var httpResponse = req.CreateResponse(HttpStatusCode.OK);
        await httpResponse.WriteAsJsonAsync(connectionInfo);
        
        return new NegotiateGroupAction
        {
            SignalRGroupAction = signalRGroupAction,
            HttpResponseData = httpResponse  
        };
    }

    [Function(nameof(SendToGroup))]
    [SignalROutput(HubName = "hub")]
    public async Task<SignalRMessageAction> SendToGroup([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
    {
        return new SignalRMessageAction("newMessage")
        {
            GroupName = "testGroup",
            Arguments = new[] { await req.ReadAsStringAsync() },
        };
    }
    
    [Function(nameof(Broadcast))]
    [SignalROutput(HubName = "hub")]
    public async Task<SignalRMessageAction> Broadcast([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
    {
        return new SignalRMessageAction("newMessage")
        {
            Arguments = new[] { await req.ReadAsStringAsync() },
        };
    }


// POCO to support multiple output bindings, in this case, HttpResponseData + SignalRGroupAction
public class NegotiateGroupAction
{
    [SignalROutput(HubName = "deploymentlogs", ConnectionStringSetting = "AzureSignalRConnectionString")]
    public SignalRGroupAction SignalRGroupAction { get; set; }
    public HttpResponseData HttpResponseData { get; set; }
}

When changing the log level to TRACE I can see the calls to the SignalR Service api are there and getting OK responses. So the output bindings do work. Still, no message is received when sending a message to a group.

Issue Analytics

  • State:open
  • Created 2 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Y-Sindocommented, Aug 3, 2023

@vlflorian at the point of negotiation, the SignalR client connection is not established yet. Therefore, it’s no way for SignalR service to know that the current client who makes the HTTP request is linked to the user ‘user1’. By specifying a user ID in the negotiation, a claim indicating the username is set in the JWT access token returned to the client. Only in this way, when the client comes to the service and establishes a SignalR connection with the JWT, the service will link the SignalR connection with that user name.

0reactions
vlfloriancommented, Aug 3, 2023

@Y-Sindo thank you for the explanation, that makes sense.

I would suggest this to be added somewhere in the documentation, though. It’s hard enough already to find usable docs and actual samples for SignalR with Azure Functions dotnet isolated, and even then basic things like these aren’t mentioned anywhere.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to send a message to the connected user when ...
The following code successfully receives "connected" events from signalR upstream when a user connects, proving that the configuration in .
Read more >
Azure Functions SignalR Service output binding
Send to a group. You can send a message only to connections that have been added to a group by setting the group...
Read more >
Invoking azure functions server SignalR group ...
I am trying to invoke azure functions server SignalR group ... Above code snippet/article refers to SignalR output binding to send messages ......
Read more >
Sending Per User Notifications using Azure Functions ...
In this post, let's have a look at how to send user Notifications using Azure Functions SignalR Service output binding. Check it out!...
Read more >
Azure SignalR binding spec
By default the Azure SignalR output binding will broadcast messages to all connected users. To narrow the audience there are two options, both...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found