We are using the Azure SignalR service from functions to send messages back to our UI and all is working without issue. But I can't find a definitive answer on how long lived the ServiceManager or HubContext should be.
At the moment each time we want to send a message to the UI we call a class we have written which does the following:
using var serviceManager = return new ServiceManagerBuilder().WithOptions(option =>
{
option.ConnectionString = _connectionString;
option.ServiceTransportType = ServiceTransportType.Persistent;
})
.WithNewtonsoftJson()
.BuildServiceManager();
await using var hubContext = await serviceManager .CreateHubContextAsync(hubName, System.Threading.CancellationToken.None);
await hubContext.Clients.Group(group).SendAsync(method, message);
This all works fine, but we are creating a new ServiceManager and ServiceHubContext every time we send a message.
The samples I have looked at do not include running in functions where we inject a service which handles publishing. Should either of these be Singleton? The functions we have are processing data and sending updates in a loop so we may send 100s of messages in a single function.