GithubHelp home page GithubHelp logo

Comments (10)

BrennanConroy avatar BrennanConroy commented on July 17, 2024 1

Yeah, I have a different workaround for you:

public class MyHub : Hub
{
    private readonly IHubContext<MyHub> _context;
    public MyHub(IHubContext<MyHub> context)
    {
        // capture an IHubContext<> that isn't tied to the Hub lifetime
        _context = context;
    }

    public Task SendMessage(string message)
    {
        // Capture the connectionId of the client so we can use Clients.Caller from outside the Hub
        var connectionId = Context.ConnectionId;
        Task.Run(async () =>
        {
            await Task.Delay(5000);
            // The IHubContext<THub> doesn't have Clients.Caller because it isn't associated with a connection
            // So we use Clients.Client(connectionId) to do the exact same thing
            await _context.Clients.Client(connectionId).SendAsync("Send", "testing");
        });

        return Task.CompletedTask;
    }

from signalr.

saibaskaran57 avatar saibaskaran57 commented on July 17, 2024 1

Hi @BrennanConroy ,

Thanks for your assistance.

I have tried the workaround and it's working great.

Hopefully, it will get considered to have it supported on SignalR instead.

from signalr.

muratg avatar muratg commented on July 17, 2024

This is by design in current release.

We'll consider https://github.com/aspnet/SignalR/issues/3133 in 3.0.

from signalr.

muratg avatar muratg commented on July 17, 2024

@BrennanConroy could you add the workaround here?

from signalr.

BrennanConroy avatar BrennanConroy commented on July 17, 2024

You can achieve long running tasks by using the IHubContext<Hub> class. See https://docs.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-2.2 for some reference on the usage.

from signalr.

saibaskaran57 avatar saibaskaran57 commented on July 17, 2024

Thanks for the confirmation.

As mentioned by @BrennanConroy, the workaround is to send via Middleware, Controllers. Our clients would be expected to use SignalR Hub to stream down their messages.

I have a thought in mind as below:

Assume the long-running task which is a service call(can be in milliseconds/seconds) and can be concurrent messages sent in.

Does the below workaround recommended to be applied since Websocket is Full-duplex?

Server-side code :

public Task SendMessage(string message)
{
      this.HandleMessageAsync(message).ConfigureAwait(false);

      return Task.CompletedTask;
}
private async Task HandleMessageAsync(string message)
{
     await Task.Delay(5000); // to simulate long running tasks(e.g. service/database calls/processing)
     await Clients.Caller.SendAsync("ReceiveMessage", message);
}

The above does not block each message and can be sent concurrently.

Please give a shout-out, if this is a terrible idea.

from signalr.

BrennanConroy avatar BrennanConroy commented on July 17, 2024

Client-side code :

I'm assuming that is still server side code, and you just meant it is the code that communicates with the client.

That approach would most likely fail because you are possibly using properties of the Hub class after the Hub method has exited and the Hub will be disposed of after each method call.

from signalr.

saibaskaran57 avatar saibaskaran57 commented on July 17, 2024

@BrennanConroy

Yes, you are right. That is still the server side code. Corrected the comment.

The current design seems to be a show-stopper for our use case. Are there any other workaround on this?

from signalr.

saibaskaran57 avatar saibaskaran57 commented on July 17, 2024

Thanks for your quick response & workaround. I'll give it a try and let you know how that went.

from signalr.

samcov avatar samcov commented on July 17, 2024

Very good information guys... Thanks!

from signalr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.