unable to send content from client to server using azure functions signalR

135 Views Asked by At

we are trying to send content from the client to server using azure functions signalR implementation. however the azure function SendToUser method is not being called.

client code:

//building connection
hubConnection = new HubConnectionBuilder().WithUrl("http://localhost:7071/api").Build();

//starting connection
await hubConnection.StartAsync();

//sending data
hubConnection.InvokeAsync("SendToUser", "message", "user");

server code:

[FunctionName("Negotiate")]
public static SignalRConnectionInfo Negotiate([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req, [SignalRConnectionInfo(HubName = "ChatHub")] SignalRConnectionInfo connectionInfo, ILogger log)
{
    log.LogInformation($"connection {connectionInfo.Url} {connectionInfo.AccessToken}");
    return connectionInfo;
}

[FunctionName("SendToUser")]
public void SendToUser([HttpTrigger(AuthorizationLevel.Anonymous, "post")] obj message, HttpRequest req, [SignalR(HubName = "ChatHub")] IAsyncCollector<SignalRMessage> signalRMessage, ILogger log)
{
    log.LogInformation($"message sent by client");
}

is this the correct implementation for this ?

1

There are 1 best solutions below

0
Mohit Ganorkar On
  • To consume the messages, you will have to use the azure service trigger bindings in the function.

  • For that you will need SignalRTrigger instead of the httptrigger you have passed.

code:

[FunctionName("SendToUser")]
public void SendToUser([SignalRTrigger]InvocationContext invocationContext, string message, ILogger logger) 
{
     logger.LogInformation($"{message} "); 
}

You can refer these MSDOC on azure service trigger bindings