locked
Why do we need an await for "Client.All/xxx.some_client_method"? RRS feed

  • Question

  • User668839819 posted

    Hello all:

    I'm using SingalR for a few days and I have a question around me: Why do we use  "Client.All/xxx.some_client_method"?

    Take these as examples:

    public class ContosoChatHub : Hub
    {
        public async Task NewContosoChatMessage(string name, string message)
        {
            await Clients.All.addNewMessageToPage(name, message);
        }
    }

    And

    public class ContosoChatHub : Hub
    {
        public void NewContosoChatMessage(string name, string message)
        {
            Clients.All.addNewMessageToPage(name, message);
        }
    }

    I cannot see any differences between the two methods……What's more——methods such as 'addNewMessageToPage' should be a client one instead of a serve one, why can it be called in sync or async mode?

    Friday, October 25, 2019 8:47 AM

All replies

  • User753101303 posted

    Hi,

    This is a "proxy" method ie this is not the real client side method but it will trigger all the plumbing needed so that ultimately  the client side method named the same way is called. This way you have a simple programming model that allows to call code on one side to trigger code that runs on the other side.

    Depends on how the procedure is written. await/async allows to give back control to the system so that something else could be done rather than being blocked while waiting for a network or disk access.

    A common confusion is thinking they have to match on both sides while it is unrelated. ie  you can have an async or sync call on the client side that calls async or sync code on the server side. It just defines what the local CPU does when it needs to wait for an operation to complete.

    Friday, October 25, 2019 11:34 AM
  • User668839819 posted

    Depends on how the procedure is written. await/async allows to give back control to the system so that something else could be done rather than being blocked while waiting for a network or disk access.

    But "Client.XXX.yyy" is only a proxy method, why does it need async? It's NOT a common async method I saw before.

    Friday, October 25, 2019 11:03 PM
  • User753101303 posted

    Which SignalR version do you use? If I remember it is not required. You can use whatever makes sense for you.

    The basic idea is to favor async input/output operations are they are much slower than the CPU. It won't make necessarily a speed difference but it can still help scalability (ie the CPU is free to process other requests rather than being blocked waiting for an i/o operation to be completed).

    Monday, October 28, 2019 9:27 AM
  • User668839819 posted

    Which SignalR version do you use

    SignalR 2.2, Net framework version is 4.6

    The basic idea is to favor async input/output operations are they are much slower than the CPU.

    I know, but what makes me very strange is there's ONLY ONE statement like client.All.XXX.yyy (Your Object things). Do you think it will be much slower than CPU?

    In my mind, the await will be used when getting Object things instead of calling by referring client.All.XXX.yyy (Your Object things).

    Tuesday, October 29, 2019 1:11 AM
  • User753101303 posted

    So it would be likely https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server#how-to-define-methods-in-the-hub-class-that-clients-can-call

    A bit later you have a paragraph about when to consider using async code. As a start do this is the other way round ie if you method uses a await xxxAsync method then use an async method.

    Else go for a sync method and make this work first.

    Tuesday, October 29, 2019 8:03 AM