I have an Azure function application with SignalR triggered by Azure blob storage. The name of the blob file is the user id of the SignalR client where it should receive the content of the file. When the Azure function application is triggered it should send message to the specific client. How to set a user id in the client side to achieve this scenario? The Azure function app is C# app while the client apps are both C# and Angular.
When the UserId in SignalRMessage is set with empty string it sends to all the clients successfully. But I need to send to a specific client with matched user id.
Here's what I've done:
[Server/Function App]
[FunctionName("BlobTringgeredFunction")]
public async Task Run([BlobTrigger("file-pro/{name}")]Stream myBlob,
string name,
ILogger log,
[SignalR(HubName = "filehub")] IAsyncCollector<SignalRMessage> signalRMessages)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
var receiver = name.Split('.').First();
var responseText = GetJson(myBlob); //get myBlob content and covert it to json text
await signalRMessages.AddAsync(
new SignalRMessage
{
UserId = receiver,
Target = "newMessage",
Arguments = new[] { responseText }
});
}
[Client Side]
Where to set User Id?
var token = await GetToken();
connection = new HubConnectionBuilder()
.WithUrl(txtConnect.Text, options =>
{
options.AccessTokenProvider = () => Task.FromResult(token);
})
.Build();
connection.On<string>("newMessage", (message) =>
{
WriteLog(message);
//do something here
});
await connection.StartAsync();