Log234 / azure-functions-signalr-dotnet-isolated-demo

5 stars 1 forks source link

SignalR serverless mode and isolated functions #3

Open cbbrsc opened 3 years ago

cbbrsc commented 3 years ago

Hi, I find your project very interesting and it helped me to start migration to a .net core 3.1 function to .net 5. I'm using azure functions and a SignalR in serverless mode. I'm using Event Grid system to notify the azue function when a new connection to SignalR is opened.

Previous version code looked like:

[FunctionName("onConnection")]
        public static Task EventGridTest([EventGridTrigger] EventGridEvent eventGridEvent,
             [SignalR(HubName = "functionExecution")] IAsyncCollector<SignalRMessage> signalRMessages)
        {
            if (eventGridEvent.EventType == "Microsoft.SignalRService.ClientConnectionConnected")
            {
                var message = ((JObject)eventGridEvent.Data).ToObject<SignalREvent>();

                return signalRMessages.AddAsync(
                    new SignalRMessage
                    {
                        ConnectionId = message.ConnectionId,
                        Target = "newConnection",
                        Arguments = new[] { new ChatMessage
                            {
                                // ConnectionId is not recommand to send to client directly.
                                // Here's a simple encryption for an easier sample.
                                ConnectionId = GetBase64EncodedString(message.ConnectionId),
                            }}
                    });
            }

            return Task.CompletedTask;
        }

Any Idea how this can be migrated? Did you found somewhere documentation about this topic for .net5?

Thanks.

Log234 commented 3 years ago

Out of curiousity, how come you are using event grid instead of just listening to the SignalR OnConnected trigger? Unfortunately, there isn't much documentation yet, most of this information I've gathered from GitHub discussions and trial-and-error. I'll make sure to update the readme with links that may be helpful.

jassent commented 3 years ago

Alternatives to the event grid approach are using the Upstream feature to be notified of connections and disconnections for Signalr Service in Serverless mode. Or, the Signlar Service Rest API to directly query for connected clients.

Log234 commented 3 years ago

Alternatives to the event grid approach are using the Upstream feature to be notified of connections and disconnections for Signalr Service in Serverless mode. Or, the Signlar Service Rest API to directly query for connected clients.

I am successfully capturing these events using just the functions in ConnectionFunctions.cs. Is there any reason why one should use upstream or the Rest API instead?

jassent commented 3 years ago

I am successfully capturing these events using just the functions in ConnectionFunctions.cs. Is there any reason why one should use upstream or the Rest API instead?

I am going to need to borrow more of your code! I wasn't successful with triggers using the connected events but will try again using the ConnectionFunctions.cs as an example.