0
votes

Is it possible for a SignalR client send a message to the server and then to await for a seperate message (not a return value) from the server?

The theory;

  1. Client1 send message1 to Server and "waits" for the response.
  2. Server processes some logic
  3. Server sends message2 to Client1 and Client1 executes the waiting code.

Call to the server:

$.connection.myhub.server.myRequest(id).done(()==>{
  // myRequest is done;
  // the server has received the request but hasn't processed it yet.
  // I want put some *async* code here to do something when the server has triggered $.connection.myhub.client.myResponse(id, someParam);
});

Callback to the client:

$.connection.myhub.client.myResponse(originalId, somePassedBackValue);

Can I use Async/Await, or wrap this in a Promise somehow?

If this isn't acheivable in SignalR are there anyother socket libraries that might be used instead?

1

1 Answers

1
votes

You can do something, like the following:

Imagine you have a client that joins a group, updates a table and then notifies the client that it has joined.

Client

msgHub.server.joinGroup(id).done(function () {
    console.log("Joined Group");
    serverCallFinished = true;
})

msgHub.client.displayTable = function (table) {
    display(table);
}

Hub

public async Task JoinGroup(string practiceId)
{
    try
    {
        await Groups.Add(Context.ConnectionId, practiceId);
        //Add new table
        var table = new Table(practiceId)
        await UpdateTable("SomeGroup", table);
    }
    catch (Exception e)
    {
        throw;
    }
}

public async Task UpdateInfo(string groupName, string table)
    {
        //await some logic
        Clients.Group(groupName).updateTable(table);
    }

Update info will call the client with message2 in this case a table that it wants to display to the client. When it finishes the it will return from its awaited state by JoinGroup which will return and alert that a new user has joined a group.