Is Signal-R supported by NET MAUI?
Asked Answered
I

2

9

I've used Signal-R in a Blazor Server app, but I don't see a way to implement it in .NET MAUI. Apparently this environment doesn't support Signal-R yet, but I might be wrong.

If it is, how do I declare an IHubContext<XHub> as a service in MauiProgram.cs?

Isiahisiahi answered 21/4, 2022 at 15:29 Comment(3)
it works in Forms, so I don't know why it wouldn't work in MAUIFerrin
Are you trying to run a Signal R Hub in Maui? I can't imagine that will go well. For example, Android isn't going to let you open up a socket. If on the other hand you are trying to run a client on Maui, I can't see any problems...Bertha
I have tried on windows and MacOs Desktop and works well. Haven't tried on mobile devices.Rrhoea
S
1

I haven't tried it myself (will do later this year), but according to this project: https://github.com/matt-goldman/maui-chat it should be supported.

Sacellum answered 7/9, 2022 at 8:4 Comment(0)
R
4

Yes, Signal R Works

Setup:

Install Microsoft.AspNetCore.SignalR.Client

In your viewmodel or Page add:

using Microsoft.AspNetCore.SignalR.Client;

Declare: private readonly HubConnection _hubConnection;

On Page or ViewModal creation/constructor:

_hubConnection = new HubConnectionBuilder().WithUrl(HUB_URL).WithAutomaticReconnect().Build();

        _hubConnection.On<YourModel>("YOUR_EVENT", (item) =>
        {
            MainThread.BeginInvokeOnMainThread(async () =>
            {
                // Do a thing
            });
        });

Initialise the connection when required:

// Start signalr to get updates
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    Task.Run(async () =>
                    {
                        try
                        {
                            var connected = _hubConnection.State == HubConnectionState.Connected;
                            if (!connected)
                            {
                                await _hubConnection.StartAsync();
                                await _hubConnection.InvokeAsync("Subscribe", YOUR_IDENTIFIER);
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    });
                });

The url is something like:

SiteUrl + "hubs/Yourhub";

Where your site has something like:

app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<App.SignalR.Hubs.FilesHub>("hubs/Yourhub");

            });
Rrhoea answered 19/6, 2023 at 17:43 Comment(0)
S
1

I haven't tried it myself (will do later this year), but according to this project: https://github.com/matt-goldman/maui-chat it should be supported.

Sacellum answered 7/9, 2022 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.