1

referring to https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-signalr-service-output?tabs=in-process&pivots=programming-language-csharp#send-to-a-user this is the code to send a message to a specific user:

[FunctionName("SendMessage")]
public static Task SendMessage([HttpTrigger(AuthorizationLevel.Anonymous, "post")]
object message
[SignalR(HubName = "chat")]
IAsyncCollector<SignalRMessage> signalRMessages)
{
    return signalRMessages.AddAsync(
        new SignalRMessage
        {
            // the message will only be sent to this user ID
            UserId = "userId1",
            Target = "newMessage",
            Arguments = new [] { message }
        });
}

however, how is the message actually sent to a userId1.? how is this configured on the client side so that it is sent to userId1.? does anything have to setup in the negotiate method.?

dev_ice
  • 21
  • 3

1 Answers1

0

I think you're confusing UserID with ConnectionID.

UserID is the username you configure in your Negotiate function when establishing the connection.

Parameters in the Function App:

[SignalR(HubName = "chat", UserId = "{query.userid}")]

Client side parameters:

/api/negotiate?userid=<username>

ConnectionID is the unique identifier each connection (not user) gets assigned when establishing a connection.

A different user doesn't have an "easy" way to know that ConnectionID unless you store it in your client side App, which is not recommended.

Now, imagine you're making a chat room.

You souldn't use ConnectionId because if a user re-establishes its connection and you try to send a message to the previous one, that message will be completely lost.

Your client side App can see (assuming you're displaying the usernames on the screen) and, therefore, send messages to any user(name) using that same UserID you sent during the negotiation.

PhobosFerro
  • 737
  • 6
  • 18